7-27 家谱处理 (30分)

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

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个正整数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

数据的输入我是循环输入的,接收每个字符,距一个点最近的,且空格数比他少2的,就是他的父节点。
前三个测试点好水,最后一个测试点卡了好半天,最后一个测试点是第一个人的父节点要标记一下。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
#define N 1005
map<string,int>mp;
string a[10];
struct node{
	int parent,b;
	vector<int>v;
}s[N];
int main(){
    int n,m,i,j,x,y,sum=0,cnt=0;
    string str;
	scanf("%d %d",&n,&m);
	cin>>str;
	mp[str]=0;	
	s[0].b=0;
	s[0].parent=-1;
	for(i=1;i<n;i++){
		char ch;
		int c=0;
		if(i==1)getchar();
		while((ch=getchar())==' ') c++;
		str=ch;
		while((ch=getchar())!='\n') str=str+ch;		
		mp[str]=i;
		x=-1;
		for(j=0;j<i;j++){
			if(s[j].b==c-2){
				x=j;
			}
		}
		s[i].b=c;
		s[i].parent=x;
		s[x].v.push_back(i);
	}
	while(m--){
		for(i=1;i<=6;i++){
			cin>>a[i];
		}
		int flag=0;
		if(a[4]=="child"){
			int ch=mp[a[1]];
			int p=mp[a[6]];	
			if(s[ch].parent==p) flag=1;
		}	
		else if(a[4]=="ancestor"){
			int p=mp[a[1]];
			int ch=mp[a[6]];	
			queue<int>q;
			if(p<ch)q.push(p);
			while(!q.empty()){
				int t=q.front();q.pop();
				if(t==ch) {
					flag=1;break;
				}
				for(i=0;i<s[t].v.size();i++){
					q.push(s[t].v[i]);
				}
			}										
		}
		else if(a[4]=="sibling"){
			int x=mp[a[1]];
			int y=mp[a[6]];
			if(s[x].parent==s[y].parent&&x!=y) flag=1;		
			
		}
		else if(a[4]=="parent"){
			int p=mp[a[1]];
			int ch=mp[a[6]];	
			if(s[ch].parent==p) flag=1;
		}
		else if(a[4]=="descendant"){
			int ch=mp[a[1]];
			int p=mp[a[6]];	
			queue<int>q;
			if(ch>p)q.push(p);
			while(!q.empty()){
				int t=q.front();q.pop();
				if(t==ch) {
					flag=1;break;
				}
				for(i=0;i<s[t].v.size();i++){
					q.push(s[t].v[i]);
				}
			}
		}
		else continue;
		if(flag) puts("True");
		else puts("False");
	}
	return 0;
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值