hdu 1075 What Are You Talking About(字典树)
2013-07-18 15:19
417 查看
刚学的字典树,代码写得很不熟练。写法上也没有什么特别的优化,就是以1A为第一目标!
可惜还是失败了。
少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A。不过感觉对字典树有了更深刻的理解。
代码写得不好,看看别人都是怎么写字典树的去。
可惜还是失败了。
少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A。不过感觉对字典树有了更深刻的理解。
代码写得不好,看看别人都是怎么写字典树的去。
#include<stdio.h> #include<string.h> #include<malloc.h> struct node { node *a[27]; char s[15]; }; char s1[15],s2[15]; char s[3005],ss[15]; int k; node *root; void InsertTree() { node *cur; node *s; int i,ln; ln=strlen(s2); cur=root; for(i=0;i<ln;i++) { if(i==ln-1) { if(cur->a[s2[i]-'a']!=NULL) { strcpy(cur->a[s2[i]-'a']->s,s1); return ; } else { s=(node *)malloc(sizeof(node)); memset(s->a,0,sizeof(s->a)); s->s[0]='\0'; cur->a[s2[i]-'a']=s; strcpy(s->s,s1); } } else if(cur->a[s2[i]-'a']!=NULL) cur=cur->a[s2[i]-'a']; else { s=(node *)malloc(sizeof(node)); memset(s->a,0,sizeof(s->a)); s->s[0]='\0'; cur->a[s2[i]-'a']=s; cur=s; } } return ; } int FindTree() { int i,ln; node *cur; cur=root; ln=strlen(ss); for(i=0;i<ln;i++) { if(i==ln-1) { if(cur->a[ss[i]-'a']!=NULL&&cur->a[ss[i]-'a']->s[0]!='\0') { printf("%s",cur->a[ss[i]-'a']->s); return 1; } else return 0; } else { if(cur->a[ss[i]-'a']!=NULL) cur=cur->a[ss[i]-'a']; else return 0; } } return 0; } int main() { root=(node *)malloc(sizeof(node)); memset(root->a,0,sizeof(root->a)); while(scanf("%s",s1)) { if(strcmp(s1,"START")==0) continue; else if(strcmp(s1,"END")==0) break; scanf("%s",s2); InsertTree(); } getchar(); while(gets(s)) { if(strcmp(s,"START")==0) continue; else if(strcmp(s,"END")==0) break; int i; k=0; for(i=0;s[i]!='\0';i++) { if(s[i]>='a'&&s[i]<='z') { ss[k++]=s[i]; continue; } else if(k!=0) { ss[k]='\0'; if(!FindTree()) printf("%s",ss); memset(ss,0,sizeof(ss)); k=0; } printf("%c",s[i]); } if(k!=0) { ss[k]='\0'; if(!FindTree()) printf("%s",ss); } printf("\n"); } return 0; }
相关文章推荐
- HDU 1075 What Are You Talking About (字典树)
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
- [字典树] HDU 1075 - What Are You Talking About
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树 或 stl map
- 【HDU 1075】What Are You Talking About(字典树)
- hdu-1075-What Are You Talking About-(字典树)
- HDU 1075 What Are You Talking About (字典树)
- Hdu 1075 - What Are You Talking About(字典树)
- 【字典树】hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About (map 或者 字典树)
- HDU 1075 What Are You Talking About(字典树)
- HDU ACM 1075 What Are You Talking About(字典树)
- 杭电hdu 1075 What Are You Talking About 字典树的应用
- hdu 1075-What Are You Talking About(Map&&字典树)
- HDU 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About (字典树)