字典樹,感覺有點暴力。
Hat’s Words Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 4 Accepted Submission(s) : 4Font: {
ProFont('Times New Roman')
}">Times New Roman | {
ProFont('Verdana')
}">Verdana | {
ProFont('Georgia')
}">Georgia Font Size: {
ProFontAdd(-1)
}">← {
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 should 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;
}