In 1982, the famous archaeologist S. Dakota Jones secretly visited the monument and found that the chamber actually was completed, but it was kept confidential. This seemed suspicious and after some poking around, she found a hidden vault and some documents inside. Unfortunately, these documents did not make any sense and were all gibberish. She suspected that they had been written in a code, but she could not decipher them despite all her efforts.
Earlier this week when she was in the area to follow the ACM-ICPC World Finals, Dr. Jones finally discovered the key to deciphering the documents, in Connolly Hall of SDSM&T. She found a document that contains a list of translations of letters. Some letters may have more than one translation, and others may have no translation. By repeatedly applying some of these translations to individual letters in the gibberish documents, she might be able to decipher them to yield historical U.S. documents such as the Declaration of Independence and the Constitution. She needs your help.
You are given the possible translations of letters and a list of pairs of original and deciphered words. Your task is to verify whether the words in each pair match. Two words match if they have the same length and if each letter of the first word can be turned into the corresponding letter of the second word by using the available translations zero or more times.
Input
The first line of input contains two integers mm (1≤m≤5001≤m≤500) and nn (1≤n≤501≤n≤50), where mm is the number of translations of letters and nn is the number of word pairs. Each of the next mm lines contains two distinct space-separated letters aa and bb, indicating that the letter aa can be translated to the letter bb. Each ordered pair of letters (a,b)(a,b) appears at most once. Following this are nn lines, each containing a word pair to check. Translations and words use only lowercase letters ‘a’–‘z’, and each word contains at least 11 and at most 5050 letters.
Output
For each pair of words, display yes if the two words match, and no otherwise.
Sample Input 1 | Sample Output 1 |
---|---|
9 5 c t i r k p o c r o t e t f u h w p we we can the work people it of out the | yes no no yes yes |
Sample Input 2
Sample Output 2
3 3 a c b a a b aaa abc abc aaa acm bcm
yes no yes
搜索ac代码:
#include<string.h>
#include<stdio.h>
int q[123][505];
int qp[123];
char p1[50],p2[50];
char a[5],b[5];
int len1,len2;
int t[8000],tp;
int chazhao(int x,int y)
{
if(x==y)
return 1;
else
{
for(int i=0; i<qp[x]; i++)
{
int gg=0;
for(int k=0; k<tp; k++)
{
if(q[x][i]==t[k])
{
gg=1;
}
}
if(gg)
continue;
t[tp++]=q[x][i];
if(chazhao(q[x][i],y))
return 1;
}
}
return 0;
}
int panding()
{
int flag=1;
for(int i=0; i<len1; i++)
{
int x=p1[i],y=p2[i];
tp=0;
t[tp++]=x;
if(!chazhao(x,y))
{
flag=0;
break;
}
}
return flag;
}
int main()
{
int n,m,x,y;
while(~scanf("%d%d",&n,&m))
{
memset(qp,0,sizeof(qp));
for(int i=0; i<n; i++)
{
scanf("%s%s",a,b);
x=a[0],y=b[0];
q[x][qp[x]]=y;
qp[x]++;
}
for(int i=0; i<m; i++)
{
scanf("%s%s",p1,p2);
len1=strlen(p1);
len2=strlen(p2);
if(len1!=len2)
{
printf("no\n");
continue;
}
if(panding())
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
}
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[30][30];
char x[5],y[5];
char p1[55],p2[55];
void jzc()
{
int t=26;
while(t--)
{
for(int i=0; i<26; i++)
{
a[i][i]=1;
for(int j=0; j<26; j++)
{
for(int k=0; k<26; k++)
{
a[i][j]+=a[i][k]*a[k][j];
if(a[i][j])
a[i][j]=1;
}
}
}
}
}
int main()
{
int n,m,flag;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
{
scanf("%s%s",x,y);
a[x[0]-'a'][y[0]-'a']=1;
}
jzc();
for(int j=1; j<=m; j++)
{
flag=1;
scanf("%s%s",p1,p2);
int len1=strlen(p1);
int len2=strlen(p2);
if(len1!=len2)
flag=0;
for(int i=0; i<len1; i++)
{
if(!flag)
break;
if(!a[p1[i]-'a'][p2[i]-'a'])
{
flag=0;
}
}
if(flag)
printf("yes\n");
else
printf("no\n");
}
}
}