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. hdu 1253 胜利大逃亡(广搜,队列,三维,简单)

    题目 原来光搜是用队列的,深搜才用栈,我好白痴啊,居然搞错了 三维的基础的广搜题 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #in ...

  2. LCS模板,求长度,并记录子串

    //LCS模板,求长度,并记录子串  //亦可使用注释掉的那些代码,但所用空间会变大 #include<iostream> #include<cstring> #include ...

  3. QAQ高精度模板笔记√

    #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #i ...

  4. 李洪强iOS开发之OC语言构造方法

    OC语言构造方法 一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情,1)使用alloc方法 ...

  5. 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例

    配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...

  6. PowerDesigner模型设计

    原文:PowerDesigner模型设计 绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的 ...

  7. linux下centos安装android sdk最新全面教程【可行】

    1.此教程解决了当你配置完android sdk的环境变量以后,可能在命令中打入adb还是提示错误.现在给大家提供最新解决方法,此方法同时可以解决最新的centos 6.5版本 2.本教程支持最新ja ...

  8. C++:String类

    String类 1.使用String类必须在程序的开始包括头文件string,即要有如下语句:#include<string> 2.string类字符串对象的使用方法与其他对象一样stri ...

  9. 设置MySQL主从同步

    1. 配置主服务器 1.1 编辑my.cnf文件,配置主服务器ID. [mysqld] log-bin=mysql-bin server-id=1relay-log = relay-bin relay ...

  10. UVa 11722 (概率 数形结合) Joining with Friend

    高中也做个这种类似的题目,概率空间是[t1, t2] × [s1, s2]的矩形,设x.y分别代表两辆列车到达的时间,则两人相遇的条件就是|x - y| <= w 从图形上看就是矩形夹在两条平行 ...