Topic links
Test instructions
Enter a well-known string and a B-pending string. Ask if the string to be detected can be changed by a known string and only one letter will be changed. You can output yes, otherwise no.
Ideas:
Stores a known string with the trie tree. DFS changes one letter to see if it can match.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>using namespacestd;Const intmax=6*1e5+Ten;CharStr[max];intval[max],ch[max][3];intSz//total number of nodesvoidInsert () {intu=0, n=strlen (str); for(intI=0; i<n;i++){ intc=str[i]-'a'; if(!ch[u][c]) {//node does not existmemset (Ch[sz],0,sizeof(Ch[sz])); VAL[SZ]=0;//The previous node is not a word node now.ch[u][c]=sz++;//new Node} u=CH[U][C];//go down.} Val[u]=1;//This node is a word node}BOOLDfsintRtintPintDintLen) { if(d>1){ return false; } if(p==Len) { if(d==1&&Val[rt]) { return true; } Else{ return false; } } for(intI=0;i<3; i++){ if(Ch[rt][i]) {//child nodes exist if(Dfs (ch[rt][i],p+1, D+ (i!=str[p]-'a') , Len) {return true; } } } return false;}intMain () {intb; while(~SCANF ("%d%d",&a,&b)) {memset (Val,0,sizeof(Val)); memset (CH,0,sizeof(CH)); SZ=1; for(intI=0; i<a;i++) {scanf ("%s", str); //puts (str);Insert (); } for(intI=0; i<b;i++) {scanf ("%s", str); //puts (str); if(Dfs (0,0,0, strlen (str))) {printf ("yes\n"); } Else{printf ("no\n"); } } } return 0;}
Tries tree first question Codeforces 514c-watto and mechanism