Hat’s Words

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

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
戴帽子的
 
 
 
解析:字典树。
 
 
 
#include <cstdio>
#include <cstring> char s[50005][50]; struct Node{
Node* pt[26];
bool isEnd;
}; Node* root;
Node memory[100000];
int allo; void trie_init()
{
memset(memory, 0, sizeof(memory));
allo = 0;
root = &memory[allo++];
} void trie_insert(char str[])
{
Node *p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
p->pt[id] = &memory[allo++];
}
p = p->pt[id];
}
p->isEnd = true;
} bool trie_find(char str[])
{
Node *p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
return false;
}
p = p->pt[id];
}
return p->isEnd;
} void getans(char str[])
{
Node* p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
return;
}
else{
if(p->pt[id]->isEnd && str[i+1] != '\0'){
bool ok = trie_find(str+i+1);
if(ok){
printf("%s\n", str);
return;
}
}
}
p = p->pt[id];
}
} void solve(int n)
{
trie_init();
for(int i = 0; i < n; ++i){
trie_insert(s[i]);
}
for(int i = 0; i < n; ++i){
getans(s[i]);
}
} int main()
{
int n = 0;
while(gets(s[n])){
++n;
}
solve(n);
return 0;
}

  

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(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  3. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  4. hdu 1247:Hat’s Words(字典树,经典题)

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

  5. HDU 1247 Hat's Words (map+string)

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

  6. HDU 1247 Hat’s Words (字符串匹配,暴力)

    题意: 给出一堆单词,如果有一个单词可以分成左右串两个单词,并且在所给的一堆单词中存在,就是hat词,统计所有这样的词,并按字典序输出. 思路: 注意定义,一个hat词可以被两部分已经存在的词组成,那 ...

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

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

  8. HDU 1247 Hat’s Words(字典树活用)

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

  9. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

随机推荐

  1. 被遗忘的Android mipmaps简介

    被遗忘的 Android mipmaps 简介 [导读]已经发布的 Android Studio1.1 版本是一个 bug 修复版本.在这个版本中,当你创建工程时一项改变将会吸引你的眼球.工程创建登陆 ...

  2. 地图索引 R-tree

    http://blog.csdn.net/v_JULY_v/article/details/6530142 984年,加州大学伯克利分校的Guttman发表了一篇题为“R-trees: a dynam ...

  3. java基础知识回顾之javaIO类---FileWriter和FileReader

    FileWriter类的构造方法定义如下: 1.public FileWriter(File file)throws IOException 字符流的操作比字节流操作好在一点,就是可以直接输出字符串了 ...

  4. poj 3101 Astronomy

    2个星球周期为a,b.则相差半周的长度为a*b/(2*abs(a-b)),对于n个只需求这n个 分数的最小公倍数即可! 公式: 分数的最小公倍数 = 分子的最小公倍数/分母的最大公约数 由于涉及到大数 ...

  5. cojs 疯狂的重心 疯狂的机器人 题解报告

    疯狂的重心 话说做过幻想乡战略游戏的人应该很容易切掉这道题目吧 我们考虑一棵树如果添加了一个叶子,那么其重心最多向叶子方向移动1的距离 而是否移动我们只需要记录子树中有多少个点就可以判断啦 也就是说这 ...

  6. BZOJ 3720 gty的妹子树

    块状树裸题 块状树: 首先对树进行分块,分出的每一块都是一个连通块 通常的分块的方式如下: 1.父亲所在块不满,分到父亲所在块中 2.父亲所在块满,自己单独开一个块 (貌似有更为优越的分块方式? 注意 ...

  7. eclipse中clean操作中如何将validating除去

    eclipse中去掉js validating方法:1. 删除.project文件中的 <buildSpec></buildSpec>中的:<buildCommand&g ...

  8. [Unity菜鸟] Time

    1. Time.deltaTime 增量时间 以秒计算,完成最后一帧的时间(秒)(只读) 帧数所用的时间不是你能控制的.每一帧都不一样,游戏一般都是每秒60帧,也就是updata方法调用60次(假如你 ...

  9. 创业草堂之一:创业的Idea是怎样产生的?

    “创业”,在很多人的想象中,就是两个小伙子在车库里.或者在学生寝室里,侃出了一个Idea,然后找到了一个投资人或VC,经过几句话讲解,VC拍手叫绝,10钟内当场拍板,砸下了2000万美金,然后两小伙子 ...

  10. WCF入门(十一)---WCF安全

    一个强大的WCF服务安全系统,拥有两种安全模式或级别预期的客户端可以访问的服务.这是常见的分布式事务的安全威胁正在放缓,在很大程度上由WCF决定. 关键的安全功能 WCF服务有四个主要的安全功能,如下 ...