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. 这些情况下onReume不应该是你的选择

    面试Android程序员的时候问过以下几个基本问题,得到的回答经常不尽人意: 1, Activity A跳转到Activity B,Activity B完成后,Activity A要刷新一下自己的数据 ...

  2. 解析JSON插入数据库

    <?php header("Content-Type:text/html;charset=utf-8"); include_once('./mysql.php'); $fil ...

  3. replace、replaceAll、replaceFirst的区别详解

    String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...

  4. 有向图寻找(一个)奇环 -- find an oddcycle in directed graph

    /// the original blog is http://www.cnblogs.com/tmzbot/p/5579020.html , automatic crawling without l ...

  5. Objective C 内存管理[转]

    1  配对原则 alloc – release new – release retain - release copy – release 2  new和alloc-init的区别 (1)区别只在于a ...

  6. offset求结构体成员的偏移量

    [代码]  C++ Code  12345678910111213141516171819202122232425262728293031   /* version: 1.0 author: hell ...

  7. java web 学习 --第十天(Java三级考试)

    第九天学习内容:http://www.cnblogs.com/tobecrazy/p/3470195.html 需求概述 创建程序模块,读取并显示数据库中的书籍信息.当前有如下需求:需要显示数据库中所 ...

  8. 手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)

      权当数据结构与算法分析的学习手记     系数为一的幂级数部分和公式 ∑ n2 = 12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6 = O(n3) ∑ n3 =  ...

  9. bing壁纸xml地址

    http://www.bing.com/gallery/?src=livesino# http://www.bing.com/HPImageArchive.aspx?format=xml&id ...

  10. DB2 SQLCODE 大全

    DB2错误信息sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 ...