人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:
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的倍数,反应到可能不是2的倍数,于是就写了个不是2的倍数的版本,没想到还是过不了最后一个点,后面看到别人写的,就感觉空格数肯定是2的倍数,我…
最后一段代码核心:
第一个祖先特殊处理下
space/2记录上一层的父亲
space/2-1就是它的父亲
初始版本:
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
void divideString(vector<string> &res, string &str) //分割单词
{
int cnt = 0;
for (int f = 0, r = 0; r < (int)str.length(); r++) //分割字符串为单词
{ //记录每个单词的长度
if (str[r] == ' ') //遇到空格,分割单词
{
res.emplace_back(str.substr(f, cnt));
f = r + 1;
cnt = 0;
}
else if (r == (int)str.length() - 1) //处理最后一个单词的情况
{
res.emplace_back(str.substr(f, cnt + 1));
}
else
{
cnt++;
}
}
}
int main()
{
unordered_map<string, string> rela; //键-值:儿子-父亲
int N, M, i;
cin >> N >> M;
getchar(); //回收第一行数字的换行
vector<string> parent(N+1, "end");
for (i = 0; i < N; i++)
{
string tem;
int space = 0, index = 0;
getline(cin, tem);
/*为什么不能用cin >> tem;
因为cin输入string会自动把前面的空格去掉,且一遇到空格,string就结束录入了
例如:
abcd。string类里面不会含有前两个空格
abcd efg。string类里面只有abcd*/
for (auto it : tem)
{
if (it != ' ')
break;
space++;
}
string sub = tem.substr(space, tem.length() - space);
while (space != 0 && space != 1) //计算2个空格一组的个数
{
space /= 2;
index++;
}
rela[sub] = parent[index];
parent[index + 1] = sub; //下标代表空格组数的父节点,parent[1]代表两个空格的父节点
}
for (i = 0; i < M; i++)
{
vector<string> single;
string tem;
getline(cin, tem);
divideString(single, tem);
string x, y, r;
x = single.front();
y = single.back();
r = single[3];
if (r == "child")
{
if (rela[x] == y)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r == "parent")
{
if (rela[y] == x)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r == "sibling")
{
if (rela[x] == rela[y])
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r == "descendant")
{
string tem = rela[x]; //遍历边或节点时,一定要重新设置一个tem来遍历
while (tem != y && tem != "end")
{
tem = rela[tem];
}
if (tem == y)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r == "ancestor")
{
string tem = rela[y]; //遍历边或节点时,一定要重新设置一个tem来遍历
while (tem != x && tem != "end")
{
tem = rela[tem];
}
if (tem == x)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
}
return 0;
}
空格不是2的倍数的版本:
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include<algorithm>
using namespace std;
void divideString(vector<string> &res, string &str) //分割单词
{
int cnt = 0;
for (int f = 0, r = 0; r < (int)str.length(); r++) //分割字符串为单词
{ //记录每个单词的长度
if (str[r] == ' ') //遇到空格,分割单词
{
res.emplace_back(str.substr(f, cnt));
f = r + 1;
cnt = 0;
}
else if (r == (int)str.length() - 1) //处理最后一个单词的情况
{
res.emplace_back(str.substr(f, cnt + 1));
}
else
{
cnt++;
}
}
}
int main()
{
unordered_map<string, string> rela; //键-值:儿子-父亲
unordered_map<int, int> location;
location[0] = 1;
int N, M, i;
cin >> N >> M;
getchar(); //回收第一行数字的换行
vector<string> parent(N + 1, "end");
int preSpace = 0;
int layer = 1;
for (i = 0; i < N; i++)
{
string tem;
int space = 0;
getline(cin, tem);
/*为什么不能用cin >> tem;
因为cin输入string会自动把前面的空格去掉,且一遇到空格,string就结束录入了
例如:
abcd。string类里面不会含有前两个空格
abcd efg。string类里面只有abcd*/
space = count(tem.begin(), tem.end(), ' ');//统计空格数
string sub = tem.substr(space);//获取名字
if (preSpace < space) //到下一层
{
layer++; //向下一层
parent[layer] = sub; //增加向下一层的长辈
location[space] = layer; //空格数一致的都是这一层
}
else
{
parent[location[space]] = sub; //相同层的,更新空格数相同的长辈
}
rela[sub] = parent[location[space] - 1]; //父亲等于上一层的parent,
/*
a
b
c
a是
*/
preSpace = space;
}
for (i = 0; i < M; i++)
{
vector<string> single;
string tem;
getline(cin, tem);
divideString(single, tem);
string x, y, r;
x = single.front();
y = single.back();
r = single[3];
if (r[0] == 'c')
{
if (rela[x] == y)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r[0] == 'p')
{
if (rela[y] == x)
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r[0] == 's')
{
if (rela[x] == rela[y])
{
cout << "True" << endl;
}
else
{
cout << "False" << endl;
}
}
else if (r[0] == 'd')
{
string tem = rela[x]; //遍历边或节点时,一定要重新设置一个tem来遍历
while (tem != y && tem != "end")//从x往上找父亲
{
tem = rela[tem];
}
if (tem == "end")
{
cout << "False" << endl;
}
else
{
cout << "True" << endl;
}
}
else if (r[0] == 'a')
{
string tem = rela[y]; //遍历边或节点时,一定要重新设置一个tem来遍历
while (tem != x && tem != "end")//从y向上找父亲
{
tem = rela[tem];
}
if (tem == "end")
{
cout << "False" << endl;
}
else
{
cout << "True" << endl;
}
}
}
return 0;
}
测试点全过的版本:
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
void divideString(vector<string> &res, string &str) //分割单词
{
int cnt = 0;
for (int f = 0, r = 0; r < (int)str.length(); r++) //分割字符串为单词
{ //记录每个单词的长度
if (str[r] == ' ') //遇到空格,分割单词
{
res.emplace_back(str.substr(f, cnt));
f = r + 1;
cnt = 0;
}
else if (r == (int)str.length() - 1) //处理最后一个单词的情况
{
res.emplace_back(str.substr(f, cnt + 1));
}
else
{
cnt++;
}
}
}
int main()
{
unordered_map<string, string> parent; //键-值:儿子-父亲
vector<string> ss(1005);
int N, M, i;
cin >> N >> M;
getchar(); //回收第一行数字的换行
for (i = 0; i < N; i++)
{
string tem;
int space = 0;
getline(cin, tem);
/*为什么不能用cin >> tem;
因为cin输入string会自动把前面的空格去掉,且一遇到空格,string就结束录入了
例如:
abcd。string类里面不会含有前两个空格
abcd efg。string类里面只有abcd*/
space = count(tem.begin(), tem.end(), ' '); //统计空格数
string sub = tem.substr(space); //获取名字
if(space==0)
{
parent[sub] = "end";
ss[0] = sub;
}
else
{
parent[sub] = ss[space / 2 - 1];
ss[space / 2] = sub;
}
}
for (i = 0; i < M; i++)
{
vector<string> single;
string tem;
getline(cin, tem);
divideString(single, tem);
string x, y, r;
x = single.front();
y = single.back();
r = single[3];
int flag = 0;
if (r == "child")
{
if (parent[x] == y)
{
flag = 1;
}
}
else if (r == "parent")
{
if (parent[y] == x)
{
flag = 1;
}
}
else if (r == "sibling")
{
if (parent[x] == parent[y])
{
flag = 1;
}
}
else if (r == "descendant")
{
while (parent[x] != y && parent[x] != "end") //从x往上找父亲
{
x = parent[x];
}
if (parent[x] == y)
{
flag = 1;
}
}
else if (r == "ancestor")
{
while (parent[y] != x && parent[y] != "end") //从y向上找父亲
{
y = parent[y];
}
if (parent[y] == x)
{
flag = 1;
}
}
if (flag == 1)
cout << "True" << endl;
else
cout << "False" << endl;
}
return 0;
}