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. Android的一种MVP模式框架

    今天给大家分享的是一种将view的初始化和逻辑与activity分离的架构,采用的是mvp模式.但令人遗憾的是,这仅仅是一个新的思路,我在实际使用中发现其并不能完全将UI逻辑与activity分开,所 ...

  2. Ubuntu固定ip和dns配置和查看

    1.查看dns: cat /etc/resolv.conf 2.Ubuntu固定ip sudo vim /etc/network/interfaces 修改如下部分: auto p3p1 iface ...

  3. flask 链接 url_for()

    通常html的文件都放在template里面,那么静态的文件放在哪呢?staitc里面 调用 url_for('static', filename='css/styles.css', _externa ...

  4. Javaweb Servlet出现Class xxx is not a servlet错误原因

  5. putty快速设置本地代理

    sudo plink -D 127.0.0.1:8888 -l root -P 443 -pw xxx 104.xxx.xxx.xxx

  6. sublime text 个性设置

    http://stackoverflow.com/questions/13781833/sublime-text-2-how-to-change-the-font-size-of-the-file-s ...

  7. ndk学习5: ndk中使用c++

    默认情况下ndk不支持标准C++库,异常, rtti等   在ndk文档有关于C++ support的详细介绍   一. 使用C++标准库 介绍: 默认是使用最小额度的C++运行时库, 在Applic ...

  8. 使用Axis2编写webservice客户端,服务端

    1.编写客户端 Axis2开发WebService客户端 的3种方式 [参考帖子] http://blog.csdn.net/wangjinwei6912/article/details/851259 ...

  9. windows下安装Apache 64bit

    文件下载:http://pan.baidu.com/s/1c0oDjFE 一.Apache的安装 http://www.blogjava.net/greatyuqing/archive/2013/02 ...

  10. 初识Python(一)

    Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...