hdu 1247 Hat’s Words(字典树)
2016-03-26 17:56
531 查看
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9574 Accepted Submission(s): 3421
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Your output should contain all the hat’s words, one per line, in alphabetical order.
[align=left]Sample Input[/align]
a ahat hat hatword hziee word
[align=left]Sample Output[/align]
ahat hatword
[align=left]Author[/align]
戴帽子的
Recommend
题意:推断一个单词能不能有两个单词组成,能够的话就输出。
题解:全部单词建成一颗字典树,单词插入结尾记录单词的个数。查询时直接暴力拆单词推断。
#include<cstring> #include<algorithm> #include<cstdio> #include<iostream> #include<cstdlib> #define N 50020 using namespace std; char s [42]; struct Trie { int num; struct Trie *nxt[26]; Trie() { num=0; for(int i=0; i<26; i++) { nxt[i]=NULL; } } }; void Trie_Inser(Trie *p,char s[]) { int i=0; Trie *q=p; while(s[i]) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL) { q->nxt[nx]=new Trie; } i++; q=q->nxt[nx]; } q->num+=1; } bool Trie_Serch(Trie *p,char s[],int be,int en) { Trie *q=p; int i=be; while(i<=en) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL)return false; q=q->nxt[nx]; i++; } if(q->num>0)return true; return 0; } int main() { //freopen("test.in","r",stdin); Trie *p=new Trie; int id=1; while(~scanf("%s",s[id])) { Trie_Inser(p,s[id]); id++; } for(int i=1; i<id; i++) { int len=strlen(s[i]); int be=0; if(len<=1)continue; for(int en=0; en<len-1; en++) { if(Trie_Serch(p,s[i],be,en)&&Trie_Serch(p,s[i],en+1,len-1)) { printf("%s\n",s[i]); break; } } } return 0; }
相关文章推荐
- MyEclipse------executeBatch()使用方法
- 2012年的Android之旅:梦想、学习、坚持、自信、淡定
- 白帽、黑帽SEO和灰帽seo的关系
- C++面向对象编程分享05----20160326_李楚煌
- (p231)15.5-4最优二叉搜索树(n^2和n^3)
- Go语言学习(六)函数
- 第四周学习进度条
- 精准剖析白帽SEO和黑帽SEO的区别
- USACO-Section 3.3 Riding the Fences(欧拉回路)
- Python入门 之 列表生成式
- codeforces 540C Ice Cave【BFS】
- nginx基本操作指令
- <div+css页面布局课堂笔记>4---与盒子相关的CSS属性
- 初识RxJava(入门篇)
- jquery复选框 选中事件 及其判断是否被选中
- 15电气 齐振昊 实验名称:15个数排序
- hdu1257最少拦截系统
- 分析Java堆
- 支付宝公众账号商户网关的搭建, RSA密钥对生成
- 对某一个软件的注册分析