显示代码纯文本
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct trie{
trie *ch[27];
bool flag;
//trie():flag(false){}
}node[1000001],*root;
int size;
/*inline int read(){
int sum(0);
char ch(getchar());
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9'){
sum=sum*10+ch-'0';
ch=getchar();
}
return sum;
}*/
trie* newnode(){
return &node[++size];
}
void insert(char *s){
int len(strlen(s));
trie *now(root);
for(int i=0;i<len;i++){
if(now->ch[s[i]-'a']==NULL)
now->ch[s[i]-'a']=newnode();
now=now->ch[s[i]-'a'];
}
now->flag=1;
}
char s[25];
int len;
bool dfs(trie *now,int pos){
if(pos==len&&now->flag)
return true;
if(s[pos]=='*'){
if(pos==len-1){
for(int i=0;i<26;i++)
if(now->ch[i]!=NULL&&now->ch[i]->flag)
return true;
return false;
}
for(int i=0;i<26;i++)
if(now->ch[i]!=NULL)
if(dfs(now->ch[i],pos+1))
return true;
return false;
}
else{
if(now->ch[s[pos]-'a']==NULL)
return false;
return dfs(now->ch[s[pos]-'a'],pos+1);
}
}
bool find(){
trie *now(root);
return dfs(now,0);
}
int gg(){
freopen("asm_talk.in","r",stdin);
freopen("asm_talk.out","w",stdout);
root=newnode();
int m;
scanf("%d",&m);
int op;
while(m--){
scanf("%d%s",&op,s);
if(op==1)
insert(s);
else{
len=strlen(s);
if(find())
puts("YES");
else
puts("NO");
}
}//cout<<m<<endl;//while(1);
return 0;
}
int k(gg());
int main(){;}