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退出循环,否则可能会重复输出。
  代码
 #include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std; struct Tire{
bool isw; //是否是一个单词
Tire *next[];
Tire()
{
isw = false;
int i;
for(i=;i<;i++)
next[i] = NULL;
}
};
Tire root; void Insert(char word[]) //将word插入到字典树中,在最后标记这是一个单词
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i]-'a';
if(p->next[t]==NULL) //如果没有对应的节点
p->next[t] = new Tire;
p = p->next[t];
}
p->isw = true; //标记到这为止是一个单词
} bool isWord(char str[]) //判断这个字符串是否为一个单词
{
Tire *p = &root;
int i;
for(i=;str[i];i++){
int t = str[i]-'a';
if(p->next[t]==NULL) //如果没有对应的节点
return false;
p = p->next[t];
}
return p->isw;
}
char word[][]; //字典 int main()
{
int size=,i,j; //字典大小 while(scanf("%s",word[size])!=EOF){ //读入字典
//if(word[size][0]=='0') break;
Insert(word[size++]);
}
size--; //检查每一个单词,判断这个单词是否为Hat's word
for(i=;i<=size;i++){
for(j=;j<strlen(word[i]);j++){
char t[],*p=word[i];
strncpy(t,word[i],j);
t[j]='\0';
p+=j;
if(isWord(t) && isWord(p)){ //如果这两部分都是单词,说明是Hat's word
printf("%s\n",word[i]);
break;
}
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

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

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  2. hdu 1247 Hat’s Words(字典树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  4. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

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

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  6. Hdu 1247 Hat's Words(Trie树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

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

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  9. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  10. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

随机推荐

  1. BZOJ 2541: [Ctsc2000]冰原探险

    Descrption 有一些矩形障碍,碰到障碍会停下,求从一个点到另一个点的最少移动步数. Sol BFS. 因为题目的特殊性质,两个矩形没有任何相邻,起始点和终点和矩形没有相邻. 所以从一个点的移动 ...

  2. BZOJ 1419: Red is good

    Sol 期望DP. \(f[i][j]\) 表示剩下 \(i\) 张红牌, \(j\) 张黑牌的期望. 有转移方程. \(f[i][j]=0,i=0\) 没有红色牌了,最优方案就是不再翻了. \(f[ ...

  3. django缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  4. 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件

    具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...

  5. LUA require 搜索路径指定方法

    如果是一个 *.LUA 的文件, 里面用到了自己写的库, 或者第三方写的库, 但是你不想把它放到 lua 的安装目录里, 则在代码里面可以指定require搜索的路径. package.path = ...

  6. React JS快速入门教程

    翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...

  7. Eclipse CDT “Symbol NULL could not be resolved”

    在ubuntu里装的eclipse C/C++版,交叉编译程序时,总是提示Symbol NULL could not be resolved.Symbol size_t could not be re ...

  8. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  9. perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误

    今天写脚本遇到Can't use string ("bond2     Link encap:InfiniBand ") as a symbol ref while "s ...

  10. FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照

    1 FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照 2016.02.18 by 1CM 2 T.. = Timeline support 3 支持时间轴 4 .S. = ...