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
a ahat hat hatword hziee word
Sample Output
ahat hatword
#include<stdio.h> #include<malloc.h> #include<string.h> char lx[50000][50]; struct node { bool flag; struct node*next[26]; }; node root; void create(char *s) { node *p=&root,*q; int i,j; int len=strlen(s); for(i=0;i<len;i++) { int num=s[i]-'a'; if(p->next[num]==NULL) { q=(node*)malloc(sizeof(node)); q->flag=false; for(j=0;j<26;j++) { q->next[j]=NULL; } p->next[num]=q; } p=p->next[num]; } p->flag=true; } bool finder(char *s) { int i,j; for(i=0;i<strlen(s);i++) { node *p=&root; for(j=0;j<i;j++) { int num=s[j]-'a'; p=p->next[num]; } if(p->flag==true) { node *q=&root; bool is=true; for(j=i;j<strlen(s);j++) { int k=s[j]-'a'; if(q->next[k]==NULL) { is=false;break;} q=q->next[k]; } if(q->flag==true&&is==true) return true; } } return false; } int main() { // freopen("in.txt","r",stdin); int i=0,j; while(scanf("%s",lx[i])!=EOF) { create(lx[i]); i++; } for(j=0;j<i;j++) { if(finder(lx[j])) printf("%s\n",lx[j]); } return 0; }