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): 7282    Accepted
Submission(s): 2639

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:  1671 1298 1800 2846 1305 


  字典树,经典题

  题意

  给你若干个单词(不超过50000个),构成一个字典,输出这个字典中所有的帽子单词(Hat‘s
word)。

  帽子单词(Hat‘s
word):由两个已在字典中出现的单词构成。

  思路

  根据输入的单词构建字典树,在插入的最后做一个这是“单词”的标记。我是用一个bool型来标记,默认为false,不是单词;如果为true,则说明到这个位置为止是一个单词。将所有单词记录下来,然后每一个单词将它拆分成两份,分别判断这两份是否为一个“单词”(在字典中出现过的字符串),这样每一个单词需要判断len-1次。例如,ahat,需要判断a和hat,ah和at,aha和t。第一组符合两份都是单词,所以这是一个帽子单词,输出该单词。

  注意

  如果已经判断出这个单词是一个帽子单词,别忘了break退出循环,否则可能会重复输出。

  代码


 1 #include <iostream>
2 #include <string.h>
3 #include <stdio.h>
4 using namespace std;
5
6 struct Tire{
7 bool isw; //是否是一个单词
8 Tire *next[26];
9 Tire()
10 {
11 isw = false;
12 int i;
13 for(i=0;i<26;i++)
14 next[i] = NULL;
15 }
16 };
17 Tire root;
18
19 void Insert(char word[]) //将word插入到字典树中,在最后标记这是一个单词
20 {
21 Tire *p = &root;
22 int i;
23 for(i=0;word[i];i++){
24 int t = word[i]-‘a‘;
25 if(p->next[t]==NULL) //如果没有对应的节点
26 p->next[t] = new Tire;
27 p = p->next[t];
28 }
29 p->isw = true; //标记到这为止是一个单词
30 }
31
32 bool isWord(char str[]) //判断这个字符串是否为一个单词
33 {
34 Tire *p = &root;
35 int i;
36 for(i=0;str[i];i++){
37 int t = str[i]-‘a‘;
38 if(p->next[t]==NULL) //如果没有对应的节点
39 return false;
40 p = p->next[t];
41 }
42 return p->isw;
43 }
44 char word[50010][101]; //字典
45
46 int main()
47 {
48 int size=1,i,j; //字典大小
49
50 while(scanf("%s",word[size])!=EOF){ //读入字典
51 //if(word[size][0]==‘0‘) break;
52 Insert(word[size++]);
53 }
54 size--;
55
56 //检查每一个单词,判断这个单词是否为Hat‘s word
57 for(i=1;i<=size;i++){
58 for(j=1;j<strlen(word[i]);j++){
59 char t[101],*p=word[i];
60 strncpy(t,word[i],j);
61 t[j]=‘\0‘;
62 p+=j;
63 if(isWord(t) && isWord(p)){ //如果这两部分都是单词,说明是Hat‘s word
64 printf("%s\n",word[i]);
65 break;
66 }
67 }
68 }
69 return 0;
70 }

Freecode : www.cnblogs.com/yym2013

hdu 1247:Hat’s Words(字典树,经典题),布布扣,bubuko.com

时间: 2024-08-02 02:40:51

hdu 1247:Hat’s Words(字典树,经典题)的相关文章

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

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): 8843    Accepted Submission(s): 3171 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

HDU 1247 Hat’s Words (字典树&#183;Trie)

题意  给你一个字典  输出字典中能表示成两个单词连接的所有单词 最基础的字典树应用  先把所有单词加入字典树中  标记每个结点是否为某个单词的结尾  然后查找每个单词  在树上查询过程中遇到单词结尾时  如果剩下的后缀也是一个单词  那当前查询的单词就可以是两个单词的连接了 #include <cstdio> #include <cstring> using namespace std; const int N = 50005; int n; char ss[N][20]; st

hdu 1247 Hat’s Words (字典树模板)

//那个单词是有出现的两个单词构成的 # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50

HDU 1251 统计难题(字典树模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 7 int num = 0; 8 9 struct Tr

hdoj 1247 Hat’s Words(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的字符串. 对于长度为LEN的字符串,其可能由LEN种可能的拼接可能:现在问题转化为查找能够拼接成该字符串的可能的两个字符串是否都在 输入的字符串中,使用字典树可以实现快速的字符串查找,算法复杂度为O(N*M),N为输入字符串的个数,M为字符串长度. 代码如下: #include <cstdio>

hdu 1247 Hat’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a

HDU 1251 统计难题(字典树 裸题 链表做法)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDU 1251 统计难题(字典树模板题 || map运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一