hdu 1247 Hat’s Words

简介:

Hat’s Words

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2414    Accepted Submission(s): 880


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

 

 

题意很清楚:就是给出若干个单词,找出其中能够由其它两个单词连接组合而成的单词。比如上面sample中的"ahat"能够由"a"和"hat"拼凑而成,"hatword"="hat"+"word".因此只需对每个单词进行切分,把切分得到的两个单词在原单词序列中查找,若能查到则符合条件。在这里为了降低查找的复杂度,采用Trie树存储原始单词序列。

实现代码:

/*hdu 1247 Hat's word 字典树 2011.10.16*/
#include <iostream>
#include<cstring>
#define MAX 26
using  namespace  std;
 
typedef  struct  Trie_Node
{
     bool  isWord;
     struct  Trie_Node *next[MAX];
}Trie;
 
char  s[50000][50];
 
void  insert(Trie *root, char  *word)      //插入单词
{
     Trie *p=root;
     while (*word!= '\0' )
     {
         if (p->next[*word- 'a' ]==NULL)
         {
             Trie *temp=(Trie *) malloc ( sizeof (Trie));
             for ( int  i=0;i<MAX;i++)
             {
                 temp->next[i]=NULL;
             }
             temp->isWord= false ;
             p->next[*word- 'a' ]=temp;
         }
         p=p->next[*word- 'a' ];
         word++;
     }
     p->isWord= true ;
}
 
bool  search(Trie *root, char  *word)         //查找单词是否存在
{
     Trie *p=root;
     for ( int  i=0;word[i]!= '\0' ;i++)
     {
         if (p==NULL||p->next[word[i]- 'a' ]==NULL)
             return  false ;
         p=p->next[word[i]- 'a' ];
     }
     return  p->isWord;
}
 
void  del(Trie *root)                    //释放空间
{
     for ( int  i=0;i<MAX;i++)
     {
         if (root->next[i]!=NULL)
         {
             del(root->next[i]);
         }
     }
     free (root);
}
 
 
int  main( int  argc, char  *argv[])
{
     int  i,j;
     int  count=0;
     char  str[50];
     Trie *root=(Trie *) malloc ( sizeof (Trie));
     for (i=0;i<MAX;i++)
     {
         root->next[i]=NULL;
     }
     root->isWord= false ;
     while ( scanf ( "%s" ,str)!=EOF)
     {  
         strcpy (s[count++],str);
         insert(root,str);  
     }
     for (i=0;i<count;i++)
     {  
         for (j=1;j<= strlen (s[i])-1;j++)  
         {
             char  temp1[50]={ '\0' };
             char  temp2[50]={ '\0' };
             strncpy (temp1,s[i],j);    
             strncpy (temp2,s[i]+j, strlen (s[i])-j);
             if (search(root,temp1)&&search(root,temp2))
             {
                 printf ( "%s\n" ,s[i]);
                 break ;                       //注意查找成功之后,需跳出循环,否则可能会重复打印
             }
         }
     }
     del(root);
     return  0;
}

本文转载自海 子博客园博客,原文链接:http://www.cnblogs.com/dolphin0520/archive/2011/10/16/2214231.html 如需转载自行联系原作者


相关文章
|
7月前
|
Java
hdu-4883- (Best Coder) TIANKENG’s restaurant
hdu-4883- (Best Coder) TIANKENG’s restaurant
36 0
L1-026 I Love GPLT (5 分)
L1-026 I Love GPLT (5 分)
133 0
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
96 0
|
Java
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1354    Accepted Submission(s): 496 Problem Description Mr.
1301 0
|
Java
2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】
Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s...
1277 0
|
存储 人工智能 Java
HDU 1250 Hat&#39;s Fibonacci
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11104    Accepted Submission(...
781 0
|
机器学习/深度学习