hdu 1247 Hat’s Words

来源:互联网 发布:迅龙数据注册机 编辑:程序博客网 时间:2024/05/16 06:15

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3666    Accepted Submission(s): 1390


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.
 

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
aahathathatwordhzieeword
 

Sample Output
ahathatword
trie树很明显,但是不知道有没有相同的单词输入,如下面,结果证明是没有这样的数据,否则还要记录相同元素数据的个数╮(╯▽╰)╭赤果果的多虑了,
 
input
ccccccccc
output
cccc
#include<stdio.h>#include<stdlib.h>#include<string.h>#define begin freopen("a.in","r",stdin); freopen("a.out","w",stdout);#define end   fclose(stdin); fclose(stdout);using namespace std;struct node{int flag,use; struct node *next[26]; node() {flag=0; use=0;  memset(next,NULL,sizeof(next)); }}root;char s[50001][50];void insert(char *s){int k=0; struct node *p=&root; while (s[k]!='\0') {if (p->next[s[k]-'a']==NULL)  p->next[s[k]-'a']=new node;  p=p->next[s[k]-'a'];  ++k; } p->flag=1; ++p->use;};int find(char *s){int k=0; struct node *p=&root; while ((s[k]!='\0')&&(p->next[s[k]-'a']!=NULL)) {p=p->next[s[k]-'a'];  ++k; } if (s[k]=='\0') return p->flag; return 0;};int main(){int l,i,j,sum=0,f1,f2; char s1[50],s2[50]; while (gets(s[++sum])) insert(s[sum]); for (i=1;i<=sum;i++) {l=strlen(s[i]);  for (j=1;j<l;j++)  {s1[j]='\0'; s2[l-j]='\0';   strncpy(s1,s[i],j);   strncpy(s2,s[i]+j,l-j);   if (find(s1)&&find(s2)) {printf("%s\n",s[i]); break;}  } } return 0;}

 
原创粉丝点击