数据结构课设 家谱处理 (map)
2016-03-22 22:03
495 查看
5-12 家谱处理 (30分)
人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:
家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,
在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:
研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。
N\le 1002≤N≤100)和MM(\le
100≤100),其中NN为家谱中名字的数量,MM为家谱中陈述语句的数量,输入的每行不超过70个字符。
名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进kk个空格,则下一行中名字至多缩进k+2k+2个空格。
在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中
没用二叉树。。学习了个比较牛的做法
参考博客:/article/10096024.html
人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:
John Robert Frank Andrew Nancy David
家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,
John这个家族最早的祖先,他有两个子女
Robert和
Nancy,
Robert有两个子女
Frank和
Andrew,
Nancy只有一个子女
David。
在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:
John is the parent of Robert Robert is a sibling of Nancy David is a descendant of Robert
研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。
输入格式:
输入首先给出2个正整数NN(2\leN\le 1002≤N≤100)和MM(\le
100≤100),其中NN为家谱中名字的数量,MM为家谱中陈述语句的数量,输入的每行不超过70个字符。
名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进kk个空格,则下一行中名字至多缩进k+2k+2个空格。
在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中
X和
Y为家谱中的不同名字:
X is a child of Y X is the parent of Y X is a sibling of Y X is a descendant of Y X is an ancestor of Y
输出格式:
对于测试用例中的每句陈述语句,在一行中输出True,如果陈述为真,或
False,如果陈述为假。
输入样例:
6 5
John Robert Frank Andrew Nancy DavidRobert is a child of John
Robert is an ancestor of Andrew
Robert is a sibling of Nancy
Nancy is the parent of Frank
John is a descendant of Andrew
输出样例:
True True True False False
没用二叉树。。学习了个比较牛的做法
参考博客:/article/10096024.html
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> using namespace std; int main(){ int n, m; scanf("%d %d", &n, &m); getchar(); map<string, string> f; vector<string> last(110); //预先分配好110的大小 string name; int i; int cnt = 0; for(i = 0; i < n; i++) { getline(cin, name); cnt = count(name.begin(), name.end(), ' '); //计算空格的数量 name = name.substr(cnt); if(cnt == 0) { f[name] = "root"; last[0] = name; } else { f[name] = last[cnt / 2 - 1]; //记录上一个父节点 last[cnt / 2] = name; } } string a, b, c, d; for(i = 0; i < m; i++) { cin >> a >> d >> d >> b >> d >> c; if(b[0] == 'c') { if(f[a] == c) printf("True\n"); else printf("False\n"); } else if(b[0] == 'p') { if(f[c] == a) printf("True\n"); else printf("False\n"); } else if(b[0] == 's') { if(f[c] == f[a]) printf("True\n"); else printf("False\n"); } else if(b[0] == 'a') { while(f[c] != a && f[c] != "root") c = f[c]; if(f[c] == "root") printf("False\n"); else printf("True\n"); } else if(b[0] == 'd') { while(f[a] != c && f[a] != "root") a = f[a]; if(f[a] == "root") printf("False\n"); else printf("True\n"); } } return 0; }
相关文章推荐
- poj 2777(线段树+区间染色)
- 数据结构 串匹配 好难懂
- 每周数据结构【5】:在二叉树中递归查找为data值的点
- 数据结构顺序表
- HDU 4763 数据结构之KMP+二分
- Python 数据结构与算法——deque(双端队列)
- 重学数据结构系列之——二叉树基础
- 算法与数据结构学习笔记系列——递归(1)
- HDU 4081 次小生成树变形记
- Python 数据结构与算法——拓扑排序
- 数据结构学习笔记02堆栈
- 数据结构——单链表的初操作
- 数据结构第一题线性表的各种操作SqList
- 大话数据结构--第四章 栈与队列
- 经典算法与数据结构的c++实现——快速排序
- 数据结构学习路线+笔记
- 数据结构(Data Structure)——0、数据类型
- 数据结构(看了之后还不懂我退出IT界)
- HDU 5137 数据结构之最短路
- Python 数据结构与算法——图出度和入度的计算