字典树,感觉有点暴力。
Hat’s Words
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
a ahat hat hatword hziee word
Sample Output
ahat hatword <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;
}