您的位置:首页 > 其它

[洛谷P2580]于是他错误的点名开始了(Trie树)

2017-03-29 20:05 281 查看

传送门

洛谷P2580的一个水题,用啥都能过,不过为了练习一下刚刚学会的字典树,还是认真做一下吧。

#include <cstdio>
#include <cstring>

using namespace std;

#define idx(x) x - 'a'

int n, m, nex;
struct node
{
int next[26];
int val;
}tree[1000001];

void Insert(char *s)
{
int i, rt = 0, len = strlen(s) - 1;
for(i = 0; i <= len; i++)
{
int c = idx(s[i]);
if(!tree[rt].next[c]) tree[rt].next[c] = ++nex;
rt = tree[rt].next[c];
}
tree[rt].val = 1;
}

int Find(char *s)
{
int i, rt = 0, len = strlen(s) - 1;
for(i = 0; i <= len; i++)
{
int c = idx(s[i]);
if(!tree[rt].next[c]) return 0;
rt = tree[rt].next[c];
}
if(tree[rt].val == 1)
{
tree[rt].val++;
return 1;
}
else return 2;
}

int main()
{
int i;
char str[1000001];
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%s", str);
Insert(str);
}
scanf("%d", &m);
for(i = 1; i <= m; i++)
{
scanf("%s", str);
int f = Find(str);
if(f == 0) printf("WRONG\n");
else if(f == 1) printf("OK\n");
else printf("REPEAT\n");
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: