uva 12526 - Cellphone Typing
2013-10-26 17:50
393 查看
字典树,可惜比赛的时候有两句话写倒了;
害得我调了一个小时;
今天不宜做题 = =
代码:
View Code
害得我调了一个小时;
今天不宜做题 = =
代码:
#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
相关文章推荐
- UVA 12526 Cellphone Typing (字典树)
- Cellphone Typing - UVaLive 6133 Trie树
- UVALive 6133 Cellphone Typing(字典树)
- UVA - 12526 Cellphone Typing
- Cellphone Typing 字典树
- BNU 27847——Cellphone Typing——————【字典树】
- UVA 11243 Texas Trip ------ UVALIVE 3954 Phone Cell
- Poj 3659 Cell Phone Network(树的最小支配集)
- n.boj 389 Shaking Your Cellphone(6th bupt acm problem D)
- Codeforces Round #361 (Div. 2) 689A Mike and Cellphone
- POJ 3659 Cell Phone Network
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone
- POJ-3659-Cell Phone Network
- HDU4026 Unlock the Cell Phone [状态压缩DP]
- POJ 3659 Cell Phone Network 最小支配集
- pku3659 Cell Phone Network
- codeforces689 A. Mike and Cellphone【模拟 + 思维】
- HDU 4026 Unlock the Cell Phone 状压dp(类似TSP)
- CodeForces 689A -Mike and Cellphone
- HDU1883 Phone Cell