Hat’s Words

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

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:  1251 1075 1671 1298 1800 
 
 
这题使用普通字典树,即可完成,先对单词进行插入操作,更新字典树,然后在对每个单词查询的时候将其分为两部分,一部分为前缀,一部分为后缀,随后在字典树中查询前缀与后缀能否同时存在,同时存在说明该单词有两个单词拼在一起的。
 
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; typedef struct node
{
int f;
struct node *nxt[];
}Trie; void insert(Trie *root, char *str)//字典树的更新操作
{
if (root == NULL || *str=='\0') return;
Trie *p = root;
while (*str != '\0')
{
if (p->nxt[*str - 'a'] == NULL)//当前结点为空,就在其下开26个空间
{
Trie *q= (Trie *)malloc(sizeof(Trie));//开辟内存空间
q->f = ;
for (int i = ; i < ; i++)
{
q->nxt[i] = NULL;
}
p->nxt[*str - 'a'] = q;
p = p->nxt[*str - 'a'];//使p指向新开辟的内存空间
}
else p = p->nxt[*str - 'a'];
str += ;
}
p->f = ;//在单词末尾标记
} int find(Trie *root, char *str)
{
Trie *p = root;
while (*str != '\0')
{
if (p->nxt[*str - 'a'] == NULL) return ;
p = p->nxt[*str - 'a'];
str += ;
}
return p->f;
} char cstr[][];
int main()
{
char c1[], c2[];
Trie *root = (Trie*)malloc(sizeof(Trie));//开辟根节点内存空间
root->f = ;
int i;
for (i = ; i < ; i++)
{
root->nxt[i] = NULL;
}
int cnt = ;
while (scanf("%s", cstr[cnt]) != EOF)
{
insert(root, cstr[cnt]);
cnt++;
}
memset(c1, '\0', sizeof(c1));//初始化c1,c2
memset(c2, '\0', sizeof(c2));
int j;
for (i = ; i < cnt; i++)
{
for (j = ; j < strlen(cstr[i]); j++)
{
strcpy(c1, cstr[i]);
c1[j] = '\0';//将cstr中0~j字符串复制的c1
strcpy(c2, cstr[i] + j);//将cstr中j~最后字符串复制到c2
if (find(root, c1) && find(root, c2))
{
printf("%s\n", cstr[i]);
break;
}
}
}
return ;
}
 

hdu1 247 Hat’s Words(字典树)的更多相关文章

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

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

  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. Hat’s Words(字典树)

    Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...

  4. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

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

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

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

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

  7. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...

  8. Hat’s Words(字典树的运用)

    个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...

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

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

随机推荐

  1. CCleaner如何禁用开机自动启动

    https://forum.piriform.com/topic/42073-ccleaner-starts-on-startup/ 在options-->setting里面选择开机不启动 在O ...

  2. luogu p3371 单源最短路径(dijkstral

    本来我写的对的 我就多手写了个 ios::sync_with_stdio(false); 我程序里面用了cin 还有scanf 本来想偷偷懒 我就说 我查了半天错 根本找不到的啊... 后来交了几次 ...

  3. fiddler几种功能强大的用法(二)

    参考网址:http://blog.rekfan.com/articles/228.html http://www.cnblogs.com/tugenhua0707/p/4637771.html htt ...

  4. Linux命令详解-man

    man 命令提供有关主题的参考信息,例如命令.子例程和文件.man 命令提供由名称指定的对命令的单行描述.man 命令也提供所有命令的信息,这些命令的描述包含用户指定的关键字集合. 1.命令格式: m ...

  5. Java 调用PHP的Web Service(三)

    usoap是PHP环境中的开源soap工具,算是用得比较多的一个工具了. 在utf-8环境中,nusoap可以工作得很好.但是当用于中文环境中时,nusoap经常会出现一些让人不得其解的问题. 最近一 ...

  6. hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

  7. 解决Jenkins 中无法展示 HTML 样式的问题

    问题 将本地的jmeter脚本部署到Jenkins上时,可以运行成功也可以在本地生成正确的HTML.但在Jenkins中查看HTML report时内容显示不出来. because the docum ...

  8. linux 下的php_gd2.dll

    今天写验证码时,发现要配置php.ini,单不知在哪儿,用下面的办法可以解决. <?php phpinfo(); ?> 用vim搜索字符串时,发现一个命令特别好用 /字符串 就可以搜索到字 ...

  9. BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数(min-max容斥&莫比乌斯反演)(线性多项式多个数求LCM)

    4833: [Lydsy1704月赛]最小公倍佩尔数 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 118[Submit][S ...

  10. UOJ22. 【UR #1】外星人【DP】【思维】

    LINK 题目大意 给你一个序列和一个值x 问你用某种方式对序列安排顺序之后一次对x取mod膜的最大值和方案数 首先发现一个性质 一个数之后所有比它大的数都没有贡献 考虑怎么利用这个性质? 就可以从小 ...