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. FreeSWITCH一些需求应对

    一.用户号码组 听到这个名词的时候,心中还挺迷茫,需求如下: 一个用户分配号码为800,但是这个用户有一部座机,两部手机:有人拨打800这个号码时,这个用户的所有关联终端都要振铃. 其实就是用户号码多 ...

  2. CPU的一些参数和排名

    排名的信息来源: http://itianti.sinaapp.com/index.php/cpu 一些参数的信息来源: http://ark.intel.com/ 个人关注一些的一些CPU排名: I ...

  3. 移除virbr0

    在我们使用虚拟机管理器的图形界面来安装虚拟机的时候,自动创建虚拟网桥和虚拟网卡.另外,我们很少会在一个虚拟机中再安装一个虚拟机,所以,我们可以将宿主机上的网桥删除.方法如下: # virsh net- ...

  4. TeeChart中 Line的Clear方法

    需要注意的是,如果设置了Line.Smoothed=true; 那么调用Clear是无效的,虽然清除了曲线上的点. 但是界面上的曲线,并没有消失. 所以,在每一次Line.Clear();之前,必须确 ...

  5. PCL点云库:ICP算法

    ICP(Iterative Closest Point迭代最近点)算法是一种点集对点集配准方法.在VTK.PCL.MRPT.MeshLab等C++库或软件中都有实现,可以参见维基百科中的ICP Alg ...

  6. python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理

    python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...

  7. HTML笔记(五)表单<form>及其相关元素

    表单标签<form> 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入信息的元素. 输入标签<input> 输入标签的输入类型由其类型属性type决定.常见的输入 ...

  8. Quartz.Net 调度框架配置介绍

    在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等.大家通过windows计划任务,windows服务等都实现过此类任务,甚至实现过自己的配置定制化的框架.那今天 ...

  9. ubuntu 下解决安装包依赖问题

    我们用banshee软件安装豆瓣插件来做个示例: 1.首先在Ubuntu Software Center内搜索banshee播放器 因为我之前已经安装好了,可以看到如下界面,你可以在点击该软件看到in ...

  10. iOS - OC NSCalendar 日历

    前言 @interface NSCalendar : NSObject <NSCopying, NSSecureCoding> NSCalendar 对世界上现存的常用的历法进行了封装,既 ...