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. shell 判断是否是目录

    创建一个文件和一个文件夹 touch sss mkdir d test.sh #!/bin/bash echo "enter the name:" read filename if ...

  2. Win7SDK

    1.ISO下载地址: http://www.microsoft.com/en-us/download/details.aspx?id=8442 2.可供下载的 版本有3个,网上搜到的解释: GRMSD ...

  3. Java Spring-传统AOP开发

    2017-11-10 17:25:48 Spring中通知Advice类型(增强代码): 前置通知,org.springframework.aop.MethodBeforeAdvice:方法前 后置通 ...

  4. 跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击

    跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击 一.总结 一句话总结:比如用户留言功能,用户留言中写的是网页可执行代码,例如js代码,然后这段代码在可看到这段留言的不同一户的显示上就会 ...

  5. WPF特效和例子

    https://www.cnblogs.com/AaronYang/p/4710428.html

  6. C# 字符串与字节数组相互转换

    https://www.cnblogs.com/xiaoqingshe/p/5882601.html

  7. git 沙河游戏节点图, 自由沙盒模拟git, 各类交互git命令

    git学习练习总资源链接: https://try.github.io/ (练习已通,有document) 本沙盒游戏教学:https://learngitbranching.js.org/?demo ...

  8. Tornado的cookie过期问题

    首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...

  9. Jquery倒计时源码分享

    在静态页添加显示倒计时的容器,并引用下面脚本,代入时间参数即可使用. timeoutDate——到期时间,时间格式为2014/01/01或2014/1/1 D——天 H——小时 M——分钟 S——秒 ...

  10. mac下安装apache tomcat

    目录 一. 默认版: 二. 自定义下载配置版: ———————————————————————正文—————————————————————————— 一. 默认版: ##一.mac 自带了apach ...