您的位置:首页 > 其它

家谱处理

2017-08-12 19:31 423 查看
Think:

也是 感觉 莫名其妙的 扣了5分, 应该是有的 条件没处理好吧。。。

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

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\le N\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

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

#include<bits/stdc++.h>
using namespace std;
map<string, int>Map1;
bool judge(char *name1, char *name2, char *relation)
{
int i, j;
i = Map1[name1];
j = Map1[name2];

if(strcmp(relation, "child") == 0)
{
if(i / 2 == j)
return true;
else
return false;
}

if(strcmp(relation, "parent") == 0)
{
if(j / 2 == i)
return true;
else
return false;
}

if(strcmp(relation, "ancestor") == 0)
{
while(j)
{
if (j / 2 == i)
return true;
j = j / 2;
}
return false;
}

if(strcmp(relation, "sibling") == 0)
{
if (i / 2 == j / 2)
return true;
else
return false;
}

if(strcmp(relation, "descendant") == 0)
{
int K = 0;

while(i != j && i && j)
{
K ++;
i = i / 2;
j = j / 2;
}
if(K == 1)
return false;
else
return true;
}

return false;
}

int a[1050];

int main()
{
int n, m, k;
int i;
int cnt;
char str[105];
scanf("%d %d\n",&n, &m);

int temp = 1;
int t = 1;

for (i = 1; i <= n; i ++ )
{
gets(str);
cnt = 0;
int d = strlen(str);
char KEY[105];
int DD = 0;
memset(KEY, 0, sizeof(KEY));
for (k = 0; k <= d - 1; k ++)
{
if(str[k] == ' ')
cnt ++;
else
KEY[DD ++] = str[k]; // KEY
}
if (cnt == temp)
{
Map1[KEY] = t + 1;
}
if (cn
4000
t > temp)
{
Map1[KEY] = t * 2;
}

if (cnt < temp)
{
Map1[KEY] = t / 2 + 1;
}
temp = cnt;
t = Map1[KEY];

}

while(m --)
{
char name1[105];
char name2[105];
char relation[105];
char total[105];
cnt = 0;
while(cnt != 6)
{
cin >> total;
cnt ++;
if (cnt == 1)
strcpy(name1, total);
if (cnt == 6)
strcpy(name2, total);
if (cnt == 4)
strcpy(relation, total);
}

bool flag = judge(name1, name2, relation);
if (flag == true)
cout << "True" << endl;
else
cout << "False" << endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  实例 二叉树