字典树典型应用(1247)
2016-05-20 19:59
302 查看
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12639 Accepted Submission(s): 4521
[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
/*------------------Header Files------------------*/ #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctype.h> #include <cmath> #include <stack> #include <queue> #include <deque> #include <map> #include <vector> #include <set> #include <limits.h> using namespace std; /*------------------Definitions-------------------*/ #define LL long long #define uLL unsigned long long #define PI acos(-1.0) #define INF 0x3F3F3F3F #define MOD 9973 #define MAX 105 #define lson rt<<1,l,m #define rson rt<<1|1,m+1,r /*---------------------Work-----------------------*/ char word[50050][30]; struct node { bool isWord; node *next[30]; node() { isWord=false; memset(next,0,sizeof(next)); } }; void insert(node *rt,char *s) { int i=0; node *p=rt; while(s[i]) { int temp=s[i++]-'a'; //所属分支 if(p->next[temp]==NULL) p->next[temp]=new node(); p=p->next[temp]; } p->isWord=true; } bool searchWord(node *rt,char *s) { stack<int>mystack; int i=0; node *p=rt; while(s[i]) { int temp=s[i++]-'a'; if(p->next[temp]==NULL) return false; p=p->next[temp]; if(p->isWord&&s[i]) mystack.push(i); } while(!mystack.empty()) { bool flag=true; i=mystack.top(); mystack.pop(); p=rt; while(s[i]) { int temp=s[i++]-'a'; if(p->next[temp]==NULL) { flag=false; break; } p=p->next[temp]; } if(flag&&p->isWord) return true; } return false; } void work() { int i=0; node *rt=new node(); while(scanf("%s",word[i])==1) { insert(rt,word[i]); i++; } //for(int j=0;j<i;j++) // printf("%s\n",word[j]); for(int j=0;j<i;j++) if(searchWord(rt,word[j])) printf("%s\n",word[j]); } /*------------------Main Function------------------*/ int main() { //freopen("test.txt","r",stdin); work(); return 0; }
相关文章推荐
- const char **ptr 与 char *ptr[] 的释放问题的探究
- MySQL入门--显示指定数据表的字段结构、插入指定字段的值(记录)、查看指定表中指定字段的所有记录
- 索尼Z2(L50t)刷CM12教程
- BZOJ 4553: [Tjoi2016&Heoi2016]序列
- 从Eclipse转移到IntelliJ IDEA的一点心得
- 用c++实现单向链表和双向链表
- Office10安装提示:Microsoft Office 2010 安装要求您的计算机上安装 MSXML 版本 6.10.1129.0
- 图像配准】基于灰度的模板匹配算法(一):MAD、SAD、SSD、MSD、NCC、SSDA、SATD算法
- Android之ListView优化基本
- Linux_2_Navigation
- 内容模式
- querySelectorAll对象无ID的题
- Wolf and Rabbit (hdu_1222) 欧几里德
- 【IPC通信】有名管道FIFO
- [No000097]程序员面试题集【下】
- django ajax 应用实例
- java代码中发送http请求的DnsResolver使用问题
- Java回调机制(callback)
- BZOJ 3239 Discrete Logging
- 求生之路