您的位置:首页 > 其它

uva 12526 - Cellphone Typing

2013-10-26 17:50 393 查看
字典树,可惜比赛的时候有两句话写倒了;

害得我调了一个小时;

今天不宜做题 = =

代码:

#include<cstdio>
#include<cstring>
#define maxn 2600009
using namespace std;

struct node
{
bool flag;
int cnt;
node *a[26];
} no[maxn];

char s[100];
int ans,nonocount;
node *newnode()
{
node *p=no+nonocount++;
p->flag=0;
p->cnt=0;
for(int i=0; i<26; i++)
p->a[i]=NULL;
return p;
}

void insert(node *rt,char *s)
{
int l=strlen(s);
int i=0;
while(1)
{
if(rt->a[s[i]-'a']==NULL) rt->a[s[i]-'a']=newnode();
rt=rt->a[s[i]-'a'];
rt->cnt++;
i++;
if(i==l)
{
rt->flag=1;
break;
}
}
}

void query(node *rt)
{
int cot=0;
for(int i=0; i<26; i++)
{
if(rt->a[i]!=NULL)
{
cot++;
query(rt->a[i]);
}
}
if(cot>1)
{
ans+=rt->cnt;
if(rt->flag==1)ans--;
}
else if(rt->flag==1)
{
ans+=(rt->cnt-1);
}
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
nonocount=0;
node *p=newnode();
for(int i=0; i<n; i++)
{
scanf("%s",s);
insert(p,s);
}
ans=0;
query(p);
printf("%.2lf\n",(double)(ans+n)/n);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: