字典树(Trie)模板(于是他错误的点名开始了)
字典树(Trie)模板(于是他错误的点名开始了)
原题目:
P2580 于是他错误的点名开始了 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7,maxm=1e5+7;
const int N=maxn,charset=26;
int n,m;
struct Trie{
int tot,root,nex[N][charset],flag[N];
Trie(){
memset(nex,-1,sizeof(nex));
memset(flag,0,sizeof(flag));
root = tot = 0;
}
void clear(){
memset(nex,-1,sizeof(nex));
memset(flag,0,sizeof(flag));
root = tot = 0;
}
void insert(string s){
int now=root;
for(int i=0;i<s.size();++i){
int x=s[i]-'a';
if(nex[now][x]==-1){
nex[now][x]=++tot;
}
now=nex[now][x];
}
flag[now]=1;
}
int query(string s){
int now=root;
for(int i=0;i<s.size();++i){
int x=s[i]-'a';
if(nex[now][x]==-1 ){
return 0;
}
now=nex[now][x];
}
if(flag[now]!=0 ) return flag[now]++;
return 0;
}
};
Trie T;
int main(){
cin>>n;
for(int i=0;i<n;++i){
string s;
cin>>s;
T.insert(s);
}
cin>>m;
for(int i=0;i<m;++i){
string s;
cin>>s;
int temp=T.query(s);
if(temp==0){
cout<<"WRONG"<<endl;
}else if(temp==1){
cout<<"OK"<<endl;
}else{
cout<<"REPEAT"<<endl;
}
}
}