您的位置:首页 > 其它

【POJ】2418 Hardwood Species

2014-06-27 12:11 351 查看
简单字典树。

#include <cstdio>
#include <cstring>
#include <cstdlib>

#define MAXN 128

typedef struct Trie {
int count;
Trie *next[MAXN];
Trie() {
count = 0;
for (int i=0; i<MAXN; ++i)
next[i] = NULL;
}
} Trie;

Trie root;
char buf[35];
double n = 0;

void create(char str[]) {
int i = 0, id;
Trie *p = &root, *q;

while (str[i]) {
id = str[i];
++i;
if (p->next[id] == NULL) {
q = new Trie();
p->next[id] = q;
}
p = p->next[id];
}
p->count++;
}

void dfs(Trie *t, int d) {
int i;

if (t->count) {
buf[d] = '\0';
printf("%s %.4lf\n", buf, t->count*100.0/n);
}
for (i=0; i<MAXN; ++i) {
if (t->next[i]) {
buf[d] = i;
dfs(t->next[i], d+1);
}
}
}

int main() {
while (gets(buf) != NULL) {
create(buf);
n += 1.0;
}
dfs(&root, 0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: