家谱处理(30 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。有问题联系[email protected] https://blog.csdn.net/weixin_40411846/article/details/78324725

家谱处理(30 分)

关键字:用unordered_map模拟多叉树

人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:

John
**Robert
****Frank
****Andrew
**Nancy
***David
(编辑器bug,空格显示不出来用
代替了)

家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进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个正整数N(2≤N≤100)和M(≤100),其中N为家谱中名字的数量,M为家谱中陈述语句的数量,输入的每行不超过70个字符。

名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进k个空格,则下一行中名字至多缩进k+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
David
Robert 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

思路

感觉很烦。参考http://blog.csdn.net/u013827143/article/details/28676703。
利用unordered_map存储树中各个节点,key和value均为string人名,key为结点这个人的人名,value为他的parent结点的人名。如果用普通的map就没法正常存下来,都被key的排序打乱了。代码中很精妙的用人名前的空格数cnt来确定他们是否有同一个parent结点。利用一个vector来顺序存储所有的parent结点人名。在判断语句处理的循环中,处理语句就很巧妙:将没有用的“is”“a”“an”“the”“of”重复cin输入到同一个string中,关键的relationship只判断一个首字母。判断关系中,将问parent和child的归为一类,将问parent转化为问child;将问ancestor和descendant的归为一类,将问ancestor转化为问descendant。

AC代码

/*
	Name:7-1 家谱处理(30 分)
	Author: shou1651312
	Date:2017年10月23日 23:12:57
	Description:数据结构实验3-2
	ref:
	http://blog.csdn.net/u013827143/article/details/28676703
*/
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<numeric>
#include<stack>
#include<queue>
#include<unordered_map>
using namespace::std;
vector<string>last(103);
unordered_map<string,string>pa;
const string nil("nil");
int main()
{
	int n,m;
	cin>>n>>m;
	cin.get();
	while(n--)
	{
		string s;
		getline(cin,s);
		int cnt=count(s.begin(),s.end(),' ');
		if(!cnt)
		{
			pa[s]=nil;
			last[0]=s;
		}
		else
		{
			s=s.substr(cnt);
			pa[s]=last[cnt/2-1];
			last[cnt/2]=s;
		}
	}
	const char *T="True",*F="False";
	while(m--)
	{
		string a,b,c,d;
		cin>>a>>d>>d>>b>>d>>c;	//d没用 所以反复存储 b_relationship
		switch(b[0])
		{
			case 'p':
				swap(a,c);
			case 'c':
				cout<<(pa[a]==c?T:F)<<endl;
				break;
			case 's':
				cout<<(pa[a]==pa[c]?T:F)<<endl;
				break;
			case 'a':
				swap(a,c);
			case 'd':
				while(pa[a]!=c&&pa[a]!=nil)
					a=pa[a];
				cout<<(pa[a]==nil?F:T)<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40411846/article/details/78324725
今日推荐