统计各个单词占得比例
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
int top, t=0;
struct node
{
int next[130];
int n;
}pos[3500];
struct nod
{
char s[35];
int num;
}ls[12000];
bool cmp(nod a,nod b)
{
return strcmp(a.s,b.s)<0;
}
void add(char st[])
{
int j = 0, len = strlen(st);
int i;
for( i = 0; i < len; i++)
{
int x = st[i];
if(pos[j].next[x]==-1)
pos[j].next[x]=++top;
j = pos[j].next[x];
}
if(pos[j].n == -1)
{
pos[j].n = t++;
strcpy(ls[t-1].s,st);
}
ls[pos[j].n].num++;
}
int main()
{
top = 0;
t = 0;
memset(pos,-1,sizeof(pos));
for(int i = 0; i < 12000; i++)
{
ls[i].num = 0;
}
char str[35];
int sum = 0;
while(gets(str)!=NULL)
{
add(str);
sum++;
}
sort(ls,ls+t,cmp);
for(int i = 0; i < t; i++)
printf("%s %.4f\n",ls[i].s,ls[i].num*1.0/sum*100);
return 0;
}