点击打开链接题目链接
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7870 Accepted Submission(s): 2851
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
输出所有的hat‘s words
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
struct node
{
node *next[26];
int end;
};
node *head;
char str[50500][100];
void tree_add(char *str)
{
node *t,*s=head;
int i,j;
int l=strlen(str);
for(i=0;i<l;i++)
{
int id=str[i]-'a';
if(s->next[id]==NULL)
{
t=new node;
for(j=0;j<26;j++)
{
t->next[j]=NULL;
}
t->end=0;
s->next[id]=t;
}
s=s->next[id];
}
s->end=1;
}
int tree_search(char *str)
{
int l=strlen(str);
int i,j,id;
node *s=head;
for(i=0;i<l;i++)
{
id=str[i]-'a';
if(s->next[id]==NULL)
return 0;
s=s->next[id];
}
return s->end;
}
int main()
{
int n=0;
head=new node;
int i,j;
char left[100],right[100];
for(i=0;i<26;i++)
{
head->next[i]=NULL;
}
head->end=0;
while(gets(str[n])!=NULL&&strcmp(str[n],"")!=0)
{
tree_add(str[n++]);
}
for(i=0;i<n;i++)
{
int l=strlen(str[i]);
for(j=1;j<l;j++)
{
strcpy(left,str[i]);
left[j]='\0';
strcpy(right,str[i]+j);
if(tree_search(left)&&tree_search(right))
{
puts(str[i]);
break;
}
}
}
return 0;
}