Transmission Door
Test instructions: I'll give you a dictionary and some questions, ask you. The string for each query must change one character to get a word in the dictionary.
Ideas: First construct a dictionary tree, and then search again on the line, to note that strlen can not be used every time, constant is very big!
#include <bits/stdc++.h> #define REP (i,k,n) for (int i=k;i<=n;i++) using namespace Std;const int maxn=300005, Maxm=600005;int tree[maxm][5],en[maxm];int N,m,e;char s[maxm];bool flag;bool siz[maxm];int lenth;void Add_edge (char c[ ]) {int len= (int) strlen (c); int root=0; Rep (i,0,len-1) {if (!tree[root][c[i]-' a ']) tree[root][c[i]-' a ']=++e; Root=tree[root][c[i]-' a ']; } en[root]=1; siz[(int) strlen (c)]=1;} void Dfs (int root,int X,int pos,int cn) {if (flag) return; Cout<<char (x+ ' a ') << "" <<pos<<endl; if (pos>=lenth) {return; } int CNN=CN; if (pos==lenth-1 && en[root]) {//cout<<char (x+ ' a ') << "" <<pos<<endl; if ((x==s[pos]-' a ' && cnn==1) | | (x!=s[pos]-' a ' && cnn==0)) flag=1; Return } if (Pos!=-1) {if (s[pos]-' a '!=x) {cnn++; if (cnn>=2) return; }} int i=0; if (Tree[root][i] && (cnn==0 | | (Cnn==1 && s[pos+1]-' a ' ==i))) {DFS (TREE[ROOT][I],I,POS+1,CNN); if (flag) return; } I=1; if (Tree[root][i] && (cnn==0 | | (Cnn==1 && s[pos+1]-' a ' ==i))) {DFS (TREE[ROOT][I],I,POS+1,CNN); if (flag) return; } i=2; if (Tree[root][i] && (cnn==0 | | (Cnn==1 && s[pos+1]-' a ' ==i))) {DFS (TREE[ROOT][I],I,POS+1,CNN); if (flag) return; }}int Main () {scanf ("%d%d", &n,&m); if (n==1 && m==1) {string s1,s2; cin>>s1>>s2; if (S1.size ()!=s2.size ()) {puts ("NO"); return 0; } int cnt=0; Rep (I,0,s1.size ()-1) {if (S1[i]!=s2[i]) {cnt++; } if (cnt>=2) {flag=1; Break }} if (flag | | cnt==0) puts ("NO"); Else puts ("YES"); return 0; } rep (i,1,n) {char C[MAXM]; scanf ("%s", c); Add_edge (c); }//cout<<en[0]<<endl; Rep (i,1,m) {scanf ("%s", s); lenth= (int) strlen (s); flag=0; if (siz[lenth]==0) {puts ("NO"); Continue } dfs (0,0,-1,0); if (flag) puts ("YES"); Else puts ("NO"); } return 0;}
Codeforces 514C. Watto and mechanism Problem Solving report (dictionary tree)