hdu 1247 hat‘s word
来源:互联网 发布:北京亚信智慧数据科技 编辑:程序博客网 时间:2024/05/24 22:45
题目链接
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
题意:给出若干个单词,找出其中能够由其它两个单词连接组合而成的单词。比如上面sample中的"ahat"能够由"a"和"hat"拼凑而成,"hatword"="hat"+"word".因此只需对每个单词进行切分,把切分得到的两个单词在原单词序列中查找,若能查到则符合条件。在这里为了降低查找的复杂度,采用Trie树存储原始单词序列。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ node* next[28]; int count; node():count(0){memset(next,0,sizeof(next));}};char ss[50005][30];node *root=new node;void Insert(char *str){ int i; node *t=root; int l=strlen(str); for(i=0;i<l;i++) { if(t->next[str[i]-'a']==0) { node *p=new node; t->next[str[i]-'a']=p; t=t->next[str[i]-'a']; } else { t=t->next[str[i]-'a']; } } t->count=1;}int Find(char *str){ node *t=root; int i,l=strlen(str); for(i=0;i<l;i++) { t=t->next[str[i]-'a']; if(!t) return 0; } if(t->count) return 1; else return 0;}int main(){ char str[50]; int k=0; while(scanf("%s",str)!=EOF) { Insert(str); strcpy(ss[k],str); k++; } int i,j,l; char str1[50],str2[50]; for(i=0;i<k;i++) { l=strlen(ss[i]); for(j=1;j<l;j++) { strncpy(str1,ss[i],j); str1[j]='\0'; strcpy(str2,ss[i]+j); if(Find(str1)&&Find(str2)) { printf("%s\n",ss[i]); break; } } } return 0;}/*#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct node{ int count; node *next[28]; node() { count=0; for(int i=0;i<28;i++) next[i]=NULL; }};node *root=new node;char ss[50005][30];node *p;node *q;void creatTrie(char *str){ int i,m; p=root; int len=strlen(str); for(i=0;i<m;i++) { m=str[i]-'a'; if(p->next[m]!=NULL) { p=p->next[m]; } else { q=new node; p->next[m]=q; p=q; } } p->count=1;}int find(char *str){ p=root; int i,m; int len=strlen(str); for(i=0;i<len;i++) { m=str[i]-'a'; if(p->next[m]==NULL) return 0; } if(p->count) return 1; else return 0;}int main(){ char str[50]; int k=0; while(scanf("%s",str)!=EOF) { creatTrie(str); strcpy(ss[k],str); k++; } int i,j,l; char str1[50],str2[50]; for(i=0;i<k;i++) { l=strlen(ss[i]); for(j=1;j<l;j++) { strncpy(str1,ss[i],j); str1[j]='\0'; strcpy(str2,ss[i]+j); if(find(str1)&&find(str2)) { printf("%s\n",ss[i]); break; } } } return 0;}*/
下面的程序代码存在运行错误,但是我也没改过来,哪位大神呢路过时帮忙给看看哈,不胜感激!
0 0
- hdu 1247 hat‘s word
- hdu 1247 Hat's word
- hdu 1247 hat's word (Trie树,水题)
- hdu 1247 Hat’s Words(从给的单词中找hat's word 并按字典序输出)
- 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
- iOS开发:查看应用的运行内存
- centos 下nfs配置
- supermap 学习系列(一)——加载地图(rest方式)
- MMI SS USSD扫盲贴
- Android_ICS_OMX_In_Stagefright------>2开始解码(软解)
- hdu 1247 hat‘s word
- EXCEL 导入到 SQL2005遇到的错误集
- 类作用域
- 编写一个方法 求一个字符串的字节长度
- OpenCV 2 编译及在vs2010中配置方法
- 关于@property 声明变量与自己声明变量问题
- What’s the difference between USSD, MMI and SS codes
- JLDAP《实现密码验证》
- 《机器学习基石》第2讲 学习笔记