题目链接:P2580 于是他错误的点名开始了
程序说明:
cnt 数组存储某字符串出现的次数。每次查询完后做标记,下一次再查到的时候输出REPEAT。大概是用getline读字符串处理不好回车的输入,卡了很久(输出是正确的但是全WA。。:) 还是用scanf输入吧不会出错。。。
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 500010;
int trie[N][26], cnt[N], idx;
char str[55];
void insert(char *str) {
int p = 0;
for(int i = 0; str[i]; i++) {
int u = str[i] - 'a';
if(!trie[p][u]) trie[p][u] = ++idx;
p = trie[p][u];
}
cnt[p]++;
}
int query(char *str) {
int p = 0;
for(int i = 0; str[i]; i++) {
int u = str[i] - 'a';
if(!trie[p][u]) {
printf("WRONG\n");
return 0;
}
p = trie[p][u];
}
if(cnt[p] == -1)
printf("REPEAT\n");
else
printf("OK\n");
cnt[p] = -1;
return cnt[p];
}
int main() {
int n, m;
scanf("%d", &n);
while(n--) {
scanf("%s", str);
insert(str);
}
scanf("%d", &m);
while(m--) {
scanf("%s", str);
query(str);
}
return 0;
}