hdu1247Hat’s Words(字典树---判断单词有无组合现象)
2013-08-07 16:56
309 查看
1.题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
2.参考代码:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
2.参考代码:
#include <cstdio> #include <cstring> struct node{ int flag; ///标记是否到达树的尾部 node* next[26]; node(){ flag=0; memset(next,0,sizeof(next)); } }; node* root=NULL; void build(char* s){ ///建树 node* p=root; int len=strlen(s); for(int i=0;i<len;i++) { if(!p->next[s[i]-'a']) p->next[s[i]-'a']=new node; p=p->next[s[i]-'a']; } p->flag=1; ///标记已到达单词尾部 } int find(char* s){ ///查找 node* p=root; int len=strlen(s); for(int i=0;i<len;i++) { if(!p->next[s[i]-'a']) return 0; p=p->next[s[i]-'a']; } if(p->flag) ///判断是否已经到达最后一个字母,避免只有前部分的相同 return 1; else return 0; } void Delete(node* p){ for(int i=0;i<10;i++) { if(!p->next[i]) Delete(p->next[i]); } delete p; } int main() { root=new node; int i,j,k,c=0; char str[50001][20]; // while(gets(str[c])) ///不能用gets()接收 while(~scanf("%s",str[c])) { build(str[c]); c++; } for(i=0;i<c;i++) { int len=strlen(str[i]); int count=0,x,y; char a[20],b[20]; for(j=0;j<len;j++) ///将字符串分段检索 { count++; ///计算单词个数 x=0; y=0; ///x和y是计算拆分后2个单词的个数 for(k=0;k<len;k++) { if(k<count) a[x++]=str[i][k]; else b[y++]=str[i][k]; } a[x]='\0'; ///单词最后的'\0'别忘了 b[y]='\0'; if(find(a) && find(b)) { printf("%s\n",str[i]); break; } } } Delete(root); return 0; }
相关文章推荐
- hdu1671Phone List(字典树---判断有无相同的前缀单词)
- POJ 3630 字典树 判断单词是否不覆盖
- hdu1247Hat’s Words (组合单词,字典树+DFS)
- hdu1247map+string判断是否子单词出现在字典中
- shell脚本中实现自动判断用户有无密码
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- (二) 如何判断链表中有无环
- hdu2072 单词数(字典树)
- 给定任意字符串,计算一共能组合成多少个单词bing
- nyoj 单词拼接(并查集判断连通性+欧拉路径)
- HDU-单词的前缀 字典树
- JS判断值只能是数字,字母或数字字母的组合
- 判断是否有网,以及网络有无状态变化
- Bellman-ford算法判断有无负环
- HWOJ 判断两个单词是否为变位词
- javascript 判断输入的文本的中文字数或者英文单词数目,混输一样判断
- Android判断有无网络的方法
- POJ 1860 Currency Exchange(SPFA 判断有无“正”环)
- 面试题初级-求字符串最后一个单词的长度和判断ip地址的合法性
- 使用kbhit函数判断键盘有无按键(非阻塞)