Hdu 1247 Hat’s Words
来源:互联网 发布:ubuntu安装tomcat 编辑:程序博客网 时间:2024/05/24 01:25
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11841 Accepted Submission(s): 4216
Problem 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.
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.
Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
aahathathatwordhzieeword
Sample Output
ahathatword
Author
戴帽子的
思路:简单题目,字典树的模板题目,先将所有的串都保存到树上,并且对单词进行标记,然后对每个单词进行查找,先查找前半部分,一旦找到了,就找下半部分,注意,下半部分要找到结束。
AC代码如下:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int seg_size=26;char str[50000+5][20];struct tries{ tries *next[seg_size]; bool sign; tries(){ memset(next,NULL,sizeof(next)); sign=false; }}* root;tries * create(){ tries *tmp=new tries(); return tmp;}void Insert(char *p,tries * root){ while(*p){ int id=*p-'a'; if(root->next[id]==NULL){ root->next[id]=create(); } root=root->next[id]; p++; } root->sign=true;}bool searchTwo(char *p){ tries *tmp=root; while(*p){ int id=*p-'a'; if(tmp->next[id]!=NULL){ p++; tmp=tmp->next[id]; } else return false; } if(tmp->sign) return true; return false;}bool searchOne(char *p){ tries *tmp=root; while(*p){ int id=*p-'a'; if(tmp->next[id]!=NULL){ if(tmp->sign){ if(searchTwo(p))return true; } p++;tmp=tmp->next[id]; } else return false; } return false;}int main(){ int cnt; root=new tries(); while(scanf("%s",str[cnt])!=EOF){ Insert(str[cnt],root); cnt++; } for(int i=0;i<cnt;i++){ if(searchOne(str[i])) puts(str[i]); } return 0;}
0 0
- hdu 1247 Hat’s Words
- hdu 1247 Hat's words
- hdu 1247 Hat’s Words
- HDU 1247 - Hat’s Words
- hdu 1247 Hat’s Words
- hdu 1247 Hat’s Words
- hdu 1247 Hat’s Words
- hdu 1247 Hat’s Words
- hdu 1247 Hat’s Words
- HDU 1247 Hat’s Words
- hdu 1247 Hat's Words
- hdu-1247 Hat’s Words
- HDU 1247 Hat’s Words
- HDU 1247 Hat’s Words
- hdu Hat's Words(1247)
- HDU 1247 Hat’s Words
- HDU 1247 Hat’s Words
- hdu 1247 Hat’s Words
- 关于使用NLPIR-ICTCLAS分词系统
- 获取谷歌日历日程数据
- Python 模块学习:os模块
- Hadoop 安装异常处理
- Android按返回键退出程序但不销毁,程序后台运行,同QQ退出处理方式
- Hdu 1247 Hat’s Words
- CountDownLatch、CyclicBarrier和Semaphore区别
- 网上商城SSH各个类间的关系(二)
- java特种兵读书笔记(5-2)——并发之线程安全
- Null在Sass中的妙用
- 仿半塘图片添加标签功能
- 动态修改ListView
- 【Android基础学习】Android权限
- [self presentViewController:vc animated:YES completion:nil]的相互引用问题