hdu1 247 Hat’s Words(字典树)

Hat’s Words

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


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
 
Author
戴帽子的
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1251 1075 1671 1298 1800 
 
 
这题使用普通字典树,即可完成,先对单词进行插入操作,更新字典树,然后在对每个单词查询的时候将其分为两部分,一部分为前缀,一部分为后缀,随后在字典树中查询前缀与后缀能否同时存在,同时存在说明该单词有两个单词拼在一起的。
 
 1 #include<cstdio>  
 2 #include<iostream>  
 3 #include<algorithm>  
 4 #include<cstring>  
 5 using namespace std;
 6 
 7 typedef struct node
 8 {
 9     int f;
10     struct node *nxt[26];
11 }Trie;
12 
13 void insert(Trie *root, char *str)//字典树的更新操作  
14 {
15     if (root == NULL || *str=='') return;
16     Trie *p = root;
17     while (*str != '')
18     {
19         if (p->nxt[*str - 'a'] == NULL)//当前结点为空,就在其下开26个空间
20         {
21             Trie *q= (Trie *)malloc(sizeof(Trie));//开辟内存空间  
22             q->f = 0;
23             for (int i = 0; i < 26; i++)
24             {
25                 q->nxt[i] = NULL;
26             }
27             p->nxt[*str - 'a'] = q;
28             p = p->nxt[*str - 'a'];//使p指向新开辟的内存空间  
29         }
30         else p = p->nxt[*str - 'a'];
31         str += 1;
32     }
33     p->f = 1;//在单词末尾标记
34 }
35 
36 int find(Trie *root, char *str)
37 {
38     Trie *p = root;
39     while (*str != '')
40     {
41         if (p->nxt[*str - 'a'] == NULL) return 0;
42         p = p->nxt[*str - 'a'];
43         str += 1;
44     }
45     return p->f;
46 }
47 
48 char cstr[50050][100];
49 int main()
50 {
51     char c1[100], c2[100];
52     Trie *root = (Trie*)malloc(sizeof(Trie));//开辟根节点内存空间 
53     root->f = 0;
54     int i;
55     for (i = 0; i < 26; i++)
56     {
57         root->nxt[i] = NULL;
58     }
59     int cnt = 0;
60     while (scanf("%s", cstr[cnt]) != EOF)
61     {
62         insert(root, cstr[cnt]);
63         cnt++;
64     }
65     memset(c1, '', sizeof(c1));//初始化c1,c2  
66     memset(c2, '', sizeof(c2));
67     int j;
68     for (i = 0; i < cnt; i++)
69     {
70         for (j = 1; j < strlen(cstr[i]); j++)
71         {
72             strcpy(c1, cstr[i]);
73             c1[j] = '';//将cstr中0~j字符串复制的c1
74             strcpy(c2, cstr[i] + j);//将cstr中j~最后字符串复制到c2
75             if (find(root, c1) && find(root, c2))
76             {
77                 printf("%s
", cstr[i]);
78                 break;
79             }
80         }
81     }
82     return 0;
83 }
 
原文地址:https://www.cnblogs.com/caiyishuai/p/8449848.html