您的位置:首页 > 其它

POJ 2945 - Find the Clones(字典树)

2015-05-09 10:27 405 查看
题目:

http://poj.org/problem?id=2945

题意:

n个长度为m的字符串,求出重复 1~n次的字符串的个数。

思路:

字典树。

AC.

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int M = 4e5+5000;
int wp, pi;
struct node {
int next[26], value;
bool ed;
}tree[M];
int ans[20005], cnt[20005];
char s[20005][25];

void init()
{
memset(tree, 0, sizeof(tree));
memset(cnt, 0, sizeof(cnt));
memset(ans, 0, sizeof(ans));
pi = 1;
wp = 0;
}
void inser(char *keyword, int value)
{
int index, p = 0;
for(int i = 0; keyword[i]; ++i) {
index = keyword[i] - 'A'; //这里一直写成a,>.<
if(tree[p].next[index] == 0) {
tree[p].next[index] = pi++;
}
p = tree[p].next[index];
}
if(tree[p].ed == 0) {
tree[p].value = value;
tree[p].ed = 1;
}
cnt[tree[p].value]++;
}

int main()
{
//freopen("in", "r", stdin);
int n, m;

while(~scanf("%d%d", &n, &m)) {
init();
if(n == 0 && m == 0) break;

for(int i = 0; i < n; ++i) {
scanf("%s", s[i]);
inser(s[i], wp++);
}

for(int i = 0; i < wp; ++i) {
ans[cnt[i]]++;
}

for(int i = 1; i <= n; ++i) {
printf("%d\n", ans[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ