#include <iostream>
#include <string.h>
#include <map>
#include <stdio.h>
using namespace std;
int parent[350];//记录每个名号的父亲名号
map<string,int> nametonum;//为每个名字编号
int num;//名号
string namelist[350];//把名号对应名字
string firstname,secondname;
bool in_tree()
{
if(nametonum.find(firstname)==nametonum.end())
{
cout<<"no relation"<<endl;
return false;
}
if(nametonum.find(secondname)==nametonum.end())
{
cout<<"no relation"<<endl;
return false;
}
return true;
}
bool in_same_tree()
{
int first=nametonum[firstname];
int second=nametonum[secondname];
while(parent[first])
first=parent[first];
while(parent[second])
second=parent[second];
if(second!=first)
{
cout<<"no relation"<<endl;
return false;
}
else
return true;
}
bool is_descendent()
{
int first=nametonum[firstname];
int second=nametonum[secondname];
int f=0;
while(parent[first]!=second && parent[first])
{
first=parent[first];
f++;
}
if(parent[first]==0) return false;
else
{
if(f==0) cout<<"child"<<endl;
else if(f==1) cout<<"grand child"<<endl;
else
{
for(int i=1; i<f; i++)
cout<<"great ";
cout<<"grand child"<<endl;
}
return true;
}
}
bool is_ancestor()
{
int first=nametonum[firstname];
int second=nametonum[secondname];
int s=0;
while(parent[second]!=first && parent[second])
{
second=parent[second];
s++;
}
if(parent[second]==0) return false;
else
{
if(s==0) cout<<"parent"<<endl;
else if(s==1) cout<<"grand parent"<<endl;
else
{
for(int i=1; i<s; i++)
cout<<"great ";
cout<<"grand parent"<<endl;
}
return true;
}
}
bool is_sibling()
{
if(parent[nametonum[firstname]]==parent[nametonum[secondname]])
{
cout<<"sibling"<<endl;
return true;
}
else
return false;
}
bool is_cousin()
{
int first=nametonum[firstname];
int second=nametonum[secondname];
int f1=0,s1=0;
while(parent[first])
{
first=parent[first];
f1++;
}
while(parent[second])
{
second=parent[second];
s1++;
}
first=nametonum[firstname];
second=nametonum[secondname];
int f2=0,s2=0;
if(f1>s1)
{
for(int i=0; i<f1-s1; i++)
{
first=parent[first];
f2++;
}
f2++;
s2++;
while(parent[first]!=parent[second])
{
first=parent[first];
second=parent[second];
f2++;
s2++;
}
cout<<s2-1<<" cousin removed "<<f2-s2<<endl;
}
else if(f1<s1)
{
for(int i=0; i<s1-f1; i++)
{
second=parent[second];
s2++;
}
f2++;
s2++;
while(parent[first]!=parent[second])
{
first=parent[first];
second=parent[second];
f2++;
s2++;
}
cout<<f2-1<<" cousin removed "<<s2-f2<<endl;
}
else
{
f2++;
s2++;
while(parent[first]!=parent[second])
{
first=parent[first];
second=parent[second];
f2++;
s2++;
}
cout<<f2-1<<" cousin"<<endl;
}
return true;
}
int main()
{
// freopen("debug\\in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
// freopen("debug\\out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中
while(cin>>firstname>>secondname)
{
if(firstname=="no.child" && secondname=="no.parent") break;
if(nametonum.find(firstname)==nametonum.end())//是个新出现的名字
{
nametonum[firstname]=++num;
namelist[num]=firstname;
}
if(nametonum.find(secondname)==nametonum.end())//是个新出现的名字
{
nametonum[secondname]=++num;
namelist[num]=secondname;
}
parent[nametonum[firstname]]=nametonum[secondname];
}
while(cin>>firstname>>secondname)
{
if(firstname==secondname)
cout<<"no relation"<<endl;
if(!in_tree())
continue;
if(!in_same_tree())
continue;
if(is_ancestor())
continue;
if(is_descendent())
continue;
if(is_sibling())
continue;
if(is_cousin())
continue;
}
return 0;
}
这题把great和grand弄混……找个半天……
后get新技能:先在project里面新建debug文件夹,建立in和out文本文件,把数据复制到in里面,结果就在out里面了,看//后的代码~