fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool maps[26][26];
  5. void WarshallsAlgorithm(){
  6.  
  7. int i, j, k;
  8.  
  9. for (k = 0; k < 26; k++)
  10. {
  11. for (i = 0; i < 26; i++)
  12. {
  13. for (j = 0; j < 26; j++)
  14. {
  15. maps[i][j] = (maps[i][j] || (maps[i][k] && maps[k][j]));
  16. }
  17. }
  18. }
  19. }
  20. int main(){
  21. int t;
  22. cin>>t;
  23.  
  24. while(t--){
  25. string s1,s2;
  26. cin>>s1>>s2;
  27. int m;
  28. cin>>m;
  29.  
  30. for(int i=0;i<26;i++){
  31. for(int j=0;j<26;j++){
  32. maps[i][j] = 0;
  33. }
  34. }
  35.  
  36. for(int k=0;k<m;k++){
  37. string li;
  38. cin>>li;
  39.  
  40. int ap = li[0]-97;
  41. int bp = li[3]-97;
  42. maps[ap][bp] = 1;
  43.  
  44. }
  45. WarshallsAlgorithm();
  46. int len1=s1.length();
  47. int len2=s2.length();
  48. if(len1!=len2){
  49. cout<<"NO"<<endl;
  50. }
  51. else{
  52. bool check=true;
  53. for(int kn=0;kn<len1;kn++){
  54. int spa = s1[kn]-97;
  55. int spb = s2[kn]-97;
  56. if(spa!=spb){
  57.  
  58. if(maps[spa][spb]!=1){
  59. check=false;break;
  60. }
  61. }
  62.  
  63. }
  64. if(!check){cout<<"NO"<<endl;}
  65. else{cout<<"YES"<<endl;}
  66. }
  67.  
  68. }
  69. return 0;
  70. }
  71.  
  72.  
  73.  
Success #stdin #stdout 0s 3480KB
stdin
3
aa
bb
1
a->b
ab
ba
2
a->b
b->a
a
b
1
b->a
stdout
YES
YES
NO