hdu 1247 Hat’s Words Trie树(+测试数据)
2015-08-13 16:30
162 查看
Hat’s Words
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1247
Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
a
ahat
hat
hatword
hziee
word
Sample Output
ahat
hatword
题意:在一些字符串中,找到这样字符串:由两个其他的字符串构成(也可能是由同一个单词两次构成 )!
祝猿们AC愉快!第一次写博客....文笔不好。 只因这题虐我伤痕累累,(呜呜~>_<~+)好不容易写出来了,就总结了一下..这也预示着我的博客之旅的开启! √(─皿─)√
自己写的几组测试数据:
Input:
a
aa
aaa
Output:
aa
aaa
Input:
ab
cd
abcdef
abcd
Output:
abcd
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1247
Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
a
ahat
hat
hatword
hziee
word
Sample Output
ahat
hatword
题意:在一些字符串中,找到这样字符串:由两个其他的字符串构成(也可能是由同一个单词两次构成 )!
祝猿们AC愉快!第一次写博客....文笔不好。 只因这题虐我伤痕累累,(呜呜~>_<~+)好不容易写出来了,就总结了一下..这也预示着我的博客之旅的开启! √(─皿─)√
自己写的几组测试数据:
Input:
a
aa
aaa
Output:
aa
aaa
Input:
ab
cd
abcdef
abcd
Output:
abcd
#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include <string.h> #define N 26 #define PI acos(-1.0) using namespace std; typedef struct Trie { int sum; int flag; struct Trie *next ; }; Trie *root=NULL; Trie *Newnode()/*初始化树*/ { Trie *p=(Trie *)malloc(sizeof(Trie)); p->sum=0; p->flag=0; for(int i=0; i<N; i++) p->next[i]=NULL; return p; } void creatTire(char *s)/*建树 这个可以作为模版*/ { int i,len=strlen(s); Trie *p=root; for(i=0; i<len; i++) { int k=s[i]-'a'; if(p->next[k]==NULL) p->next[k]=Newnode(); p=p->next[k]; p->sum++; } p->flag=1;/*标记一个字符串的最后一个单词*/ } void del(Trie *p)/*清除树*/ { int i; if(p==NULL) return ; for(i=0; i<N; i++) if(p->next[i]!=NULL) del(p->next[i]); free(p); } int findTire(char *s) { Trie *p=root; int n=strlen(s),i; for(i=0; i<n; i++) { int j,k; k=s[i]-'a'; p=p->next[k]; if(p==NULL) break; if(p->flag==1&&i!=n-1)/*找到了匹配的前半部分*/ { j=i+1; Trie *q=root; for(; j<n; j++) /*找后半部分*/ { k=s[j]-'a'; q=q->next[k]; if(q==NULL) break; if(q->flag==1&&j==n-1)/*j==n-1表示完全匹配*/ return 2;/*返回2说明找到符合题意的了*/ } //return 1;/*在这错了好久,要是找到了前半部分但是没找到匹配的后半部分应该把前半部分的下标向后移,继续判断!*/ } } return 0; } char s[51000][110]; int main() { int n,i=0,j; root=Newnode();/*这儿要注意了 别忘了加上这一句*/ while(scanf("%s",s[i])!=EOF)/*有一些人说用ges错 就不太清楚了*/ { creatTire(s[i]); i++; } for(j=0; j<i; j++) { n=findTire(s[j]); if(n==2) printf("%s\n",s[j]); } //del(root); return 0; }
相关文章推荐
- 生理学教学系统——心脏泵血流程
- iScroll内部textarea滑动不了bug
- Unity3D中实现动态加载Resources目录外的资源
- Hadoop安装
- getRequestDispatcher()与sendRedirect()的区别
- SOAPUI 安装
- nyoj38 布线问题
- hdu5384(AC自动机+纪录重复单词出现的次数)
- js禁止右键功能,禁止复制
- mergeIDE
- AIX下查看CPU、内存信息-prtconf
- Win10 Mobile预览版10512 原生壁纸中暗藏微软诞生日彩蛋
- 创建数据库,表空间,建表,用户的区别
- Callable和Future
- iOS数据持久化---对象归档
- javascript中 if(变量)和if(变量=true)的区别
- iOS 系统定位的说明与使用
- 【bzoj1002】【FJOI2007】【轮状病毒】【找规律】
- Nature:深度学习的现在和未来:DeepLearning、无监督学习、NLP
- hdu4333 扩展KMP