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;
}
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---2945 Find the Clones[字典树-简单题(多例输入注意删除)]
- POJ2945 Find the Clones
- POJ 2945 Find the Clones Hash
- POJ2945——Find the Clones
- POJ 2945 Find the Clones (Trie树 两种姿势)
- poj 2945 Find the Clones
- poj 2945 Find the Clones (map+string,hash思维)
- POJ 2945 Find the Clones
- (使用STL自带的排序函数进行排序7.3.6)POJ 2945 Find the Clones拷贝相同次数的文本出现了多少次()
- poj 2945 Find the clones (tire树)
- [poj] 2945 Find the Clones
- POJ 2945 Find the Clones 水
- poj 2945 Find the Clones 用trie树实现
- poj 2945 Find the Clones
- poj 2945 Find the Clones trie树的简单应用
- poj 2945 Find the Clones
- POJ 2945 Find the Clones
- poj 2945 Find the Clones
- poj_2945 Find the Clones (Trie树 内存分配)
- POJ2945 Find the Clones(暴力 & 字典树)