trie 树 模板

时间:2022-04-19 21:33:58
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN=5e6+5;
int nume,n,m;
char s[55],fff[4][20]={" ","OK","WRONG","REPEAT"};
struct node{
int cnt,child[26];
bool end,vis;
}trie[MAXN];
void ins(){
int u=0,len=strlen(s);
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!trie[u].child[v]) trie[u].child[v]=++nume;
u=trie[u].child[v];
trie[u].cnt++;
}
trie[u].end=1;
}
int query(){
int len=strlen(s),u=0;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(!trie[u].child[v]) return 2;
u=trie[u].child[v];
}
if(!trie[u].end) return 2;
if(trie[u].vis) return 3;
trie[u].vis=1;
return 1;
}
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
int main(){
freopen("in.txt","r",stdin);
n=init();
for(int i=1;i<=n;i++) {
scanf("%s",s);
ins();
}
m=init();
for(int i=1;i<=m;i++){
scanf("%s",s);
printf("%s\n",fff[query()]);
}
fclose(stdin);
return 0;
}