Hat’s Words

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

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>
#include<cstdlib>
typedef struct node
{
struct node *child[];
bool tail;
}Trie;
char mat[][];
void Insert(char *s,Trie *root)
{
int i,pos,j;
Trie *cur=root,*curnew;
for(i=;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]==NULL)
{
curnew=(Trie *)malloc(sizeof(Trie));
for(j=;j<;j++)
curnew->child[j]=NULL;
curnew->tail=false;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
}
cur->tail=true;
}
bool check(char *s,Trie *root)
{
int i,pos;
Trie *cur=root;
for(i=;s[i]!='\0';i++)
{
pos=s[i]-'a';
if(cur->child[pos]==NULL) return ;
cur=cur->child[pos];
}
if(cur->tail==)return ;
return ;
}
bool query(char *s,Trie *root)
{
int i,pos;
Trie *cur=root;
for(i=;s[i]!='\0';i++)
{
pos=s[i]-'a';
if(cur->child[pos]==NULL) return ;
else
if(cur->tail==&&check(s+i,root))
return ;
cur=cur->child[pos];
}
return ;
}
int main()
{
int i=,j;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
Trie *root=(Trie *)malloc(sizeof(Trie));
root->tail=;
for(j=;j<;j++)
root->child[j]=NULL;
while(scanf("%s",mat[i])!=EOF){
Insert(mat[i],root);
i++;
}
for(j=;j<i;j++)
{
if(query(mat[j],root))
printf("%s\n",mat[j]);
}
return ;
}

hdu--(1247)Hat’s Words(trie树)的更多相关文章

  1. 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 ...

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

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

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

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

  4. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  5. HDU1247 - Hat’s Words(Trie树)

    题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...

  6. hdu 1251:统计难题[【trie树】||【map】

    <题目链接> 统计难题                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131 ...

  7. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  8. HDU - 1251 统计难题(Trie树)

    有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...

  9. HDU 1251 统计难题 (Trie树模板题)

    题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...

  10. HDU 1247 Hat’s Words(字典树变形)

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

随机推荐

  1. c#中struct和class的区别 详细[转]

    转自:http://blog.csdn.net/justlovepro/archive/2007/11/02/1863734.aspx 有这么几点不同: 1.struct 是值类型,class是对象类 ...

  2. V-rep学习笔记:转动关节1

    V-REP(Virtual Robot Experimentation Platform),是全球领先的机器人及模拟自动化软件平台.V-REP让使用者可以模拟整个机器人系统或其子系统(如感测器或机械结 ...

  3. [HRBUST1472]Coin(dp,计数)

    题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1472 题意:给n个硬币,面值随意.问恰好凑成m元的种类数(去掉重复). dp(i,j,k ...

  4. zoj 2107&&hdu 1007最近点对问题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds   ...

  5. 构建工具之 - Gradle一般使用常见问答

    Gradle借助Groovy语言作为其配置脚本,使得Gradle变的十分强大,几乎是无所不能,我们在掌控这个强大且功能繁多的工具时也比较困难和凌乱,因此本文记录一些使用过程常见的配置,以备不时之需! ...

  6. Getuserpassword

    将[新注册的用户的用户名和密码]保存到服务端本地 /*将注册成功的用户名和密码保存到本地*/ /*定位*/ File f = new File("D:/lab_2/用户名和密码.qq&quo ...

  7. Android_安装GooglePlay

    百度搜索:“google play 安装” http://jingyan.baidu.com/article/cbf0e500f4645b2eab28935a.html http://samsungb ...

  8. poj2187Beauty Contest(凸包直径)

    链接 利用旋转卡壳 参考博客http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <iostream> ...

  9. sscanf的用法(转)

    队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...

  10. ssh免密码登陆及其原理

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...