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): 5178    Accepted Submission(s): 1934

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
 
 
题目意思:首先题目要求输入一系列的字符串,然后判断哪些字符串是由其他的两个字符串组成的,若存在某个字符串是
        由其他的两个字符串组成,就输出该字符串。
题目分析:首先把输入的字符串插入到字典树中,并把字符串用数组保存起来。然后把每个字符串分解成子字符串1和子字符串2,
        分别对这两个子字符串进行查询,若这两个子字符串都存在字典树中,就输出该字符串。
 
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 typedef struct _TrieTree
 6 {
 7     bool flag;
 8     struct _TrieTree *next[26];
 9 
10 }TrieNode;
11 
12 TrieNode *CreateTrieTree()   //创建字典树
13 {
14     TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
15     node->flag = false;
16     for(int i = 0; i < 26; i++)
17         node->next[i] = NULL;
18 
19     return node;
20 }
21 
22 void InsertTrieTree(TrieNode *root, char word[]) //插入字符串
23 {
24     TrieNode *p = root;
25     int length = strlen(word);
26     for(int i = 0; i < length; i++)
27     {
28         int index = word[i] - 97;
29         if(p->next[index] == NULL)
30         {
31             p->next[index] = CreateTrieTree();
32         }
33         p = p->next[index];
34     }
35     p->flag = true;
36 }
37 
38 bool QueryTrieTree(TrieNode *root, char word[])   //查询字符串
39 {
40     TrieNode *p = root;
41     int length = strlen(word);
42     for(int i = 0; i < length; i++)
43     {
44         int index = word[i] - 97;
45         if(p->next[index] == NULL)
46             return false;
47 
48         p = p->next[index];
49     }
50 
51     if(p->flag)
52         return true;
53     return false;
54 }
55 
56 const int WORD_NUM = 50005;
57 char word[WORD_NUM][21];
58 
59 int main(int argc, char* argv[])
60 {
61     TrieNode *root = CreateTrieTree();
62 
63     int cnt = 0;
64     while(scanf("%s", word[cnt]) != EOF)
65     {
66         InsertTrieTree(root, word[cnt]);
67         cnt += 1;
68     }
69 
70     char strfirst[21];
71     char strSecond[21];
72     for(int i = 0; i < cnt; i++)
73     {
74         int length = strlen(word[i]);
75         for(int j = 1; j < length; j++)
76         {
77             memset(strfirst, 0, sizeof(strfirst));
78             memset(strSecond, 0, sizeof(strSecond));
79 
80             strncpy(strfirst, word[i], j);  //分解成两个字符串
81             strcpy(strSecond, word[i] + j);
82             
83             bool bFirst  = QueryTrieTree(root, strfirst);
84             bool bSecond = QueryTrieTree(root, strSecond);
85             
86             if(bFirst && bSecond)           //判断两个字符串是否都存在
87             {                               //若是,则输出
88                 printf("%s\n", word[i]);
89                 break;
90             }
91         }
92     }
93 
94     return 0;
95 }

 

 

posted @ 2013-05-23 22:52  Dreamcaihao  阅读(226)  评论(0编辑  收藏  举报