Secret Chamber at Mount Rushmore
, indicating that the letter a can be translated to the letter b. Each ordered pair of letters (a,b)
appears at most once. Following this are n lines, each containing a word pair to check. Translations and words use only lowercase letters ‘a’–‘z’, and each word contains at least 1 and at most 50 letters.
Output
For each pair of words, display yes if the two words match, and no otherwise.
Sample Input 1
Sample Output 1
9 5c ti rk po cr ot et fu hw pwe wecan thework peopleit ofout the
yes no no yes yes
Sample Input 2
Sample Output 2
3 3a cb aa baaa abcabc aaaacm bcm
yesnoyes
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000],b[1000];
int d[30][30];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m))
{
char x,y;
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
{
getchar();//吸收回车
scanf("%c %c",&x,&y);
d[x-'a'][y-'a']=1;
}
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
if(d[i][j])
{
for(int k=0;k<26;k++)
{
if(d[k][i])
d[k][j]=1;//有间接联系的
}
}
}
}
for(int p=1;p<=m;p++)
{
scanf("%s%s",a,b);
int e1=strlen(a);
int e2=strlen(b);
int i=0,j=0,flag=0;
while(i<e1&&j<e2&&i==j&&flag==0)
{
if(a[i]==b[j]||d[a[i]-'a'][b[j]-'a']==1)
i++,j++;
else
flag=1;
}
if(e1==e2&&flag==0)
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}