The dictionary tree is a bit violent.
Hat's words time limit: 2000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 4 accepted submission (s): 4 Font: {
Profont ('times new Roman ')
} "> Times New Roman | {
Profont ('verdana ')
} "> Verdana | {
Profont ('Georgia ')
} "> Georgia font size :{
Profontadd (-1)
} "> Detail {
Profontadd (1)
} "> → {
Objfolder ('procon ')
} "> Problem descriptiona 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 .{
Objfolder ('proinput ')
} "> Inputstandard 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 .{
Objfolder ('prooutput ')
} "> Outputyour output shoshould contain all the hat's words, one per line, in alphabetical order .{
Objfolder ('prosamplein ')
} "> Sample input
aahathathatwordhzieeword
{
Objfolder ('prosampleout ')
} "> Sample output
ahathatword<code>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char str[50005][100],str1[100],str2[100];
struct dirtree
{
struct dirtree *child[26];
bool hash;
};
struct dirtree *root;
void Insert(char*source)
{
int i,j,len;
len=strlen(source);
if(len==0)return ;
struct dirtree *current,*newnode;
current=root;
for(i=0;i<len;i++)
{
if(current->child[source[i]-'a']!=0)
{
current=current->child[source[i]-'a'];
if(i==len-1)
{
current->hash=true;
break;
}
}
else
{
newnode=(struct dirtree*)malloc(sizeof(struct dirtree));
for(j=0;j<26;j++)
{
newnode->child[j]=0;
newnode->hash=false;
}
current->child[source[i]-'a']=newnode;
current=newnode;
if(i==len-1)
{
current->hash=true;
break;
}
}
}
}
bool Find(char *source)
{
struct dirtree *current;
int len=strlen(source);
if(len==0)return 0;
current=root;
for(int i=0;i<len;i++)
{
if(current->child[source[i]-'a']!=0)
current=current->child[source[i]-'a'];
else
return false;
}
return current->hash;
}
int main()
{
int len = 0;
int i, j, k, len1;
root = (struct dirtree *)malloc(sizeof(struct dirtree));
for(i = 0; i < 26; i++)
{
root->child[i] = 0;
root->hash = false;
}
while(scanf("%s",str[len])!=EOF)
{
Insert(str[len]);
len++;
}
for(i=0;i<len;i++)
{
len1=strlen(str[i]);
for(j=1;j<len1;j++)
{
for(k=0;k<j;k++)
{
str1[k]=str[i][k];
}
str1[k]='/0';
int t=0;
for(k=j;k<len1;k++)
{
str2[t++]=str[i][k];
}
str2[t]='/0';
if(Find(str1)&&Find(str2))
{
printf("%s/n",str[i]);
break;
}
}
}
//system("pause");
return 0;
}