题目链接 :https://hpuoj.com/contest/24/problem/I/
input
6
aabbcc
cdbcad
4
a c
c a
a d
b c
output
YES
此题方法很多,最容易想到的就是用邻接矩阵表示关系,转化为最短路问题,然后可以用Floyd—Warshall算法的三层for循环。还可以用广搜。。。。
标程用了一个与运算和一个或运算,很巧妙
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int n,m,e[102][102];
memset(e,0,sizeof(e));
char a[102],b[102];
scanf("%d",&n);
getchar();
scanf("%s %s",a,b);
getchar();
scanf("%d",&m);
getchar();
for(int i=0; i<m; i++){
char t1,t2;
scanf("%c %c",&t1,&t2);
e[t1-'a'+1][t2-'a'+1]=1;
getchar();
}
for(int k=1; k<=26; k++)
for(int i=1; i<=26; i++)
for(int j=1; j<=26; j++)
e[i][j]|=(e[i][k]&e[k][j]);
int f=0;
for(int i=0; i<n; i++){
if(a[i]!=b[i]){
if(!e[a[i]-'a'+1][b[i]-'a'+1]){
f=1;
break;
}
}
}
if(f)printf("NO\n");
else
printf("YES\n");
return 0;
}