Meaning of the questions:
Pattern string to n, m matching string, asked whether there was a difference of only a character string with a pattern string matching
Ideas:
Directly on Tire, normal insertion pattern string
Match to his son that node normal match, if the matching is successful, output YES
#include<iostream> #include<algorithm> #include<cstring> #include<string> using namespace std; const int maxn=1e6+10; int n,m; struct Tire{ int ch[maxn][3],val[maxn],cnt; void init() { cnt=1; memset(ch,0,sizeof(ch)); val[0]=0; } void insert(string s){ int u=0,n=s.length(); for(int i=0;i<n;i++){ int c=s[i]-'a'; if(!ch[u][c]) ch[u][c]=cnt++; u=ch[u][c]; } val[u]=cnt; } bool query(string s){ int u=0,n=s.length(),flag,k; for(int i=0;i<n;i++){ int c=s[i]-'a'; for(int j=0;j<=2;j++){ if(c==j||ch[u][j]==0) continue; int x=ch[u][j]; flag=1; for(int k=i+1;k<n;k++){ int y=s[k]-'a'; if(!ch[x][y]){ flag=0; break; } x=ch[x][y]; } if(flag&&val[x]) return true; } if(!ch[u][c]) return false; u=ch[u][c]; } return false; } }T; int main() { string temp; scanf("%d%d",&n,&m); T.init(); for(int i=1;i<=n;i++){ cin>>temp; T.insert(temp); } for(int i=1;i<=m;i++){ cin>>temp; if(T.query(temp)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }