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. [调参]batch_size的选择

    链接:https://www.zhihu.com/question/61607442/answer/440944387 首先反对上面的尽可能调大batch size的说法,在现在较前沿的视角来看,这种 ...

  2. HDU 6038 Function(思维+寻找循环节)

    http://acm.hdu.edu.cn/showproblem.php?pid=6038 题意:给出两个序列,一个是0~n-1的排列a,另一个是0~m-1的排列b,现在求满足的f的个数. 思路: ...

  3. VNC Viewer连接Cent OS 时的 复制粘帖 功能

    虽然 VNC Viewer 比起 vsphere Client 来, 感觉性能差一点. 但毕竟也是个选择. 找了一下 它的这个 功能. 运行一下 vncconfig & 就可以了. 实测好用. ...

  4. Rails 5 Test Prescriptions 第11章其他部分的测试。

    Routes✅ Helper Methods✅ Controllers and Requests✅ Simulating Requests⚠️,看之前的博客 What to Expect in a R ...

  5. 在Linux和Windows系统上安装Nginx服务器的教程

    在Linux和Windows系统上安装Nginx服务器的教程  1.在CentOS系统上安装Nginx 在 CentOS6 版本的 EPEL 源中,已经加入了 nginx 的 rpm 包,不过此 RP ...

  6. 改变Vim保存文件路径

    1. vim 有个cd命令.用来更改当前文件夹.:cd sth进入sth文件夹.这样新文件保存之后就在当前文件夹.不过如果你打开一个已经保存的文件后然后更改当前文件夹是不会改变保存路径的.你必须为:w ...

  7. FireFox和IE下使用Date来构造新Date对象的BUG

    正常方式 我们都知道可以使用new Date()来创建一个Date对象. new Date();//Date {Mon Jun 15 2015 15:53:16 GMT+0800} 也可以用new D ...

  8. python多任务的导包问题

    多线程的使用: import threading def func(x): print(x) t= threading.Thread(target=func,args=(12,)) # 线程启动 t. ...

  9. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 12

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  10. ubuntu14.04安装Android Studio出现error while loading shared libraries: libz.so.1的解决方法

    参考博客地址: http://blog.csdn.net/newairzhang/article/details/28656693 安装lib32z1就可以解决,如下: 首先,sudo apt-get ...