您的位置:首页 > 其它

poj2418Hardwood Species

2013-01-20 15:45 483 查看
http://poj.org/problem?id=2418

trie树,有非空格、字母的字符

View Code

#include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef struct node
{
int num;
struct node *next[130];
node()
{
memset(next,NULL,sizeof(next));
num = 0;
}
}trie;
int y,k;
char ss[10010][40],w[40];
void ctrie(trie *t,char *x)
{
int i,j,d,k = strlen(x);
char yy[40];
strcpy(yy,x);
trie *p = t;
for(i = 0 ; i < k ; i++)
{
d = x[i];
if(p->next[d]==NULL)
{
p->next[d] = new node;
}
p = p->next[d];
}
if(p->num==0)
{
strcpy(ss[y],yy);
y++;
}
p->num++;
}
void dfs(trie *t,int x)
{
trie *p = t;
int i;
if(p->num>0)
{
w[x] = '\0';
printf("%s %.4lf\n",w,p->num*100.0/k);
}
for(i = 1; i <= 129 ; i++)
{
if(p->next[i]!=NULL)
{
w[x] = i;
dfs(p->next[i],x+1);
}
}
}
int main()
{
int i,j;
char s[40];
trie *t = new node;
while(gets(s)!=NULL)
{
k++;
ctrie(t,s);
}
dfs(t,0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: