目录:
分析:
明显字典树版题,对于要求输出的
OK
O
K
、
REPEAT
R
E
P
E
A
T
,我们只需要对每次查询后进行累加,当下次查询到时,就可以判断+输出了
最后点下字典树的范围是
1−500000
1
−
500000
,这个500000是由
n(10000)∗字符串长度(50)
n
(
10000
)
∗
字
符
串
长
度
(
50
)
得出
代码:
// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
struct node{
int have,cnt;//have表示是否构成单词,cnt用来统计
int son[26];//顾名思义
node()
{
cnt=0;
memset(son,0,sizeof(son));
have=0;
}
}trie[500001];
int num=0;
void in(char *s)
{
int w,len=strlen(s);
int f=0;
for(int i=0;i<len;i++)
{
w=s[i]-'a';
if(!trie[f].son[w])
trie[f].son[w]=++num;
f=trie[f].son[w];
}
trie[f].have=1;
return;
}
int ask(char *s)
{
int f=0,w,len=strlen(s);
for(int i=0;i<len;i++)
{
w=s[i]-'a';
if(!trie[f].son[w]) return 3;
f=trie[f].son[w];
}
if(!trie[f].have) return 3;
if(!trie[f].cnt)
{
trie[f].cnt++;
return 1;
}
return 2;
}
int main()
{
int n=read();
char s[51];
for(int i=1;i<=n;i++)
{
scanf("%s",s);
in(s);
}
n=read();
int p;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
p=ask(s);
if(p==1) printf("OK\n");
else if(p==2) printf("REPEAT\n");
else printf("WRONG\n");
}
return 0;
}