pku 2418 Hardwood Species 字典树
生活随笔
收集整理的這篇文章主要介紹了
pku 2418 Hardwood Species 字典树
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://poj.org/problem?id=2418
字典數(shù)編號(hào),統(tǒng)計(jì)每個(gè)單詞出現(xiàn)的次數(shù),然后計(jì)算頻率:
View Code #include <iostream>#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 10007
using namespace std;
struct node
{
int flag;
node *next[100];
}*head,H[999999];
int pos;
int L;
struct mm
{
char s[33];
}pp[maxn];
int cmp(mm a,mm b)
{
return strcmp(a.s,b.s) < 0;
}
node* newnode()
{
node *q = &H[pos++];
for (int i = 0; i< 100; ++i)
q->next[i] = NULL;
q->flag = 0;
return q;
}
void insert(char *s)
{
int i,k;
//printf(">>%s\n",s);
int len = strlen(s);
node *p = head;
for (i = 0; i < len; ++i)
{
k = s[i] - 32;
if (p->next[k] == NULL)
p->next[k] = newnode();
p = p->next[k];
}
if (p->flag == 0)
{
strcpy(pp[L++].s,s);
p->flag++;
}
else
p->flag++;
}
int find(char *s)
{
int i,k;
int len = strlen(s);
node *p = head;
for (i = 0; i < len; ++i)
{
k = s[i] - 32;
if (p->next[k] == NULL)
{
return 0;
}
p = p->next[k];
}
return p->flag;
}
int main()
{
//freopen("in.txt","r",stdin);
char ch[33];
L = pos = 0;
head = &H[pos++];
int sum = 0;
while (gets(ch) != NULL)
{
sum++;
insert(ch);
}
sort(pp,pp + L,cmp);
for (int i = 0; i < L; ++i)
{
double num = find(pp[i].s);
printf("%s %.4lf\n",pp[i].s,100.0*num/(1.0*sum));
}
return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/E-star/archive/2012/04/06/2435370.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的pku 2418 Hardwood Species 字典树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FormsAuthenticationT
- 下一篇: ES 基础