L2-030 冰岛人 (25分)
2018年世界杯,冰岛队因1:1平了强大的阿根廷队而一战成名。好事者发现冰岛人的名字后面似乎都有个“松”(son),于是有网友科普如下:
冰岛人沿用的是维京人古老的父系姓制,孩子的姓等于父亲的名加后缀,如果是儿子就加 sson,女儿则加 sdottir。因为冰岛人口较少,为避免近亲繁衍,本地人交往前先用个 App 查一下两人祖宗若干代有无联系。本题就请你实现这个 App 的功能。
输入格式:
输入首先在第一行给出一个正整数 N(1<N≤10
5
),为当地人口数。随后 N 行,每行给出一个人名,格式为:名 姓(带性别后缀),两个字符串均由不超过 20 个小写的英文字母组成。维京人后裔是可以通过姓的后缀判断其性别的,其他人则是在姓的后面加 m 表示男性、f 表示女性。题目保证给出的每个维京家族的起源人都是男性。
随后一行给出正整数 M,为查询数量。随后 M 行,每行给出一对人名,格式为:名1 姓1 名2 姓2。注意:这里的姓是不带后缀的。四个字符串均由不超过 20 个小写的英文字母组成。
题目保证不存在两个人是同名的。
输出格式:
对每一个查询,根据结果在一行内显示以下信息:
若两人为异性,且五代以内无公共祖先,则输出 Yes;
若两人为异性,但五代以内(不包括第五代)有公共祖先,则输出 No;
若两人为同性,则输出 Whatever;
若有一人不在名单内,则输出 NA。
所谓“五代以内无公共祖先”是指两人的公共祖先(如果存在的话)必须比任何一方的曾祖父辈分高。
输入样例:
15
chris smithm
adam smithm
bob adamsson
jack chrissson
bill chrissson
mike jacksson
steve billsson
tim mikesson
april mikesdottir
eric stevesson
tracy timsdottir
james ericsson
patrick jacksson
robin patricksson
will robinsson
6
tracy tim james eric
will robin tracy tim
april mike steve bill
bob adam eric steve
tracy tim tracy tim
x man april mikes
输出样例:
Yes
No
No
Whatever
Whatever
NA
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define N 150001
char ch[3]={'-','1','\0'};
unsigned int hash(char *str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int h = 0;
while (*str)
{
h =( h * a + (*str++))%N;
a *= b;
}
return h;
}
int find(char s[][21],char t[21])
{
int i,k;
k=hash(t);
while(strcmp(s[k],ch)!=0)
{
if(strcmp(t,s[k])==0)
return k;
k=(k+1)%N;
}
return -1;
}
int main()
{
int n;
char s[N][21],t[N][21];
char tem1[21],tem2[21];
char temp1[21],temp2[21];
int t1[N],t2[N],top1,top2;
int i,j,k1,a,b,c,d,x1,x2,h,k2,len,len1,len2;
int m;
scanf("%d",&n);
for(i=0;i<N;i++)
{
strcpy(s[i],ch);
strcpy(t[i],ch);
}
for(i=0;i<n;i++)
{
scanf("%s",tem1);
scanf("%s",tem2);
h=hash(tem1);
while(strcmp(s[h],ch)!=0)
h=(h+1)%N;
strcpy(s[h],tem1);
strcpy(t[h],tem2);
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s",tem1);
scanf("%s",tem2);
scanf("%s",temp1);
scanf("%s",temp2);
k1=find(s,tem1);
k2=find(s,temp1);
if(k1==-1||k2==-1)
{
printf("NA\n");
}
else
{
a=strlen(t[k1]);
b=strlen(tem2);
c=strlen(t[k2]);
d=strlen(temp2);
x1=1;
if(t[k1][a-1]=='n'||t[k1][a-1]=='m')
x1=0;
x2=1;
if(t[k2][c-1]=='n'|| t[k2][c-1]=='m')
x2=0;
if(x1==x2)
printf("Whatever\n");
else if(((t[k1][a-1]=='f') &&(t[k2][c-1]=='m'))||((t[k2][c-1]=='f') &&(t[k1][a-1]=='m')))
printf("Yes\n");
else{
top1=0;
top2=0;
t1[top1]=k1;
top1++;
k1=find(s,tem2);
while(k1!=-1)
{
t1[top1]=k1;
top1++;
len1=strlen(t[k1]);
if(t[k1][len1-1]=='n')
len1=len1-4;
else
len1=len1-1;
for(j=0;j<len1;j++)
tem2[j]=t[k1][j];
tem2[j]='\0';
k1=find(s,tem2);
}
t2[top2]=k2;
top2++;
k2=find(s,temp2);
while(k2!=-1)
{
t2[top2]=k2;
top2++;
len2=strlen(t[k2]);
if(t[k2][len2-1]=='n')
len2=len2-4;
else
len2=len2-1;
for(j=0;j<len2;j++)
temp2[j]=t[k2][j];
temp2[j]='\0';
k2=find(s,temp2);
}
if(t1[top1-1]!=t2[top2-1])
{
printf("Yes\n");
continue;
}
while(top1!=0 && top2!=0 && t1[top1-1]==t2[top2-1])
{
top1--;
top2--;
}
if(top1>=4&&top2>=4)
printf("Yes\n");
else
printf("No\n");
}
}
}
return 0;
}