HDU 1247 Hat’s Words(字典树活用)
Hat’s Words
Time Limit : 2000 / 1000 MS(Java / Others) Memory Limit : 65536 / 32768 K(Java / Others)
Total Submission(s) : 18969 Accepted Submission(s) : 6689
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
题意:按字典序输入几个单词,按字典序输出由另外两个单词组成的单词
分析:先建树,后查询每个单词,如果查询到第一个原有单词再接着查询剩余部分,剩余部分也是原有单词组成则输出该单词
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char temp[50001][20];
bool vis[300001];
int t[300001][30],pos=1,num[300001];
void insert(char *s)//建树
{
int rt = 0;
int len = strlen(s);
for (int i = 0; i < len; i++)
{
int x = s[i] - 'a';
if (!t[rt][x])
t[rt][x] = pos++;
rt = t[rt][x];
}
vis[rt] = 1;//标记
}
bool search1(char *s)//查询后部分是否是已有单词构成
{
int rt = 0;
for (int i = 0; s[i]; i++)
{
int x = s[i] - 'a';
if (!t[rt][x])
return 0;
rt = t[rt][x];
}
if (vis[rt])//验证尾结点是否为查到的单词的尾结点
return 1;
else
return 0;
}
bool search(char *s)//前部分
{
int rt = 0;
for (int i = 0; s[i]; i++)
{
int x = s[i] - 'a';
if (vis[rt] && search1(s + i))//rt为前部分单词的尾结点,验证后部分是否为已有单词
return 1;
rt = t[rt][x];
}
return 0;
}
int main()
{
int i = 0;
while (~scanf("%s", temp[i]))
insert(temp[i++]);
for (int j = 0; j <i; j++)
{
//cout << temp[j] << endl;
if (search(temp[j]))
printf("%s\n", temp[j]);
}
return 0;
}
HDU 1247 Hat’s Words(字典树活用)的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- 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 ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 1247 Hat’s Words(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- python初级实战-----关于邮件发送问题
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email模块主要负责构造邮件. sm ...
- Queue和BlockingQueue的使用以及使用BlockingQueue实现生产者-消费者
Java提供了两种新的容器类型:Queue和BlockingQueue. Queue用于保存一组等待处理的元素.它提供了几种实现,包括:ConcurrentLinkedQueue,这是一个先进先出的并 ...
- java知识点4
架构篇 分布式 数据一致性.服务治理.服务降级 分布式事务 2PC.3PC.CAP.BASE. 可靠消息最终一致性.最大努力通知.TCC Dubbo 服务注册.服务发现,服务治理 分布式数据库 怎样打 ...
- 为什么要重写equals和hashcode方法
equals hashcode 当新建一个java类时,需要重写equals和hashcode方法,大家都知道!但是,为什么要重写呢? 需要保证对象调用equals方法为true时,hashcode ...
- ini文件解析c库(iniparser)【转】
转自:http://www.cnblogs.com/dyllove98/archive/2013/07/28/3221732.html 一.交叉编译ini解析库 .官方网站http://ndevill ...
- 武汉倍特威视系统有限公司 --- 安全帽识别|烟火识别|智慧工地|BVS智能视频分析
武汉倍特威视系统有限公司 --- 安全帽识别|烟火识别|智慧工地|BVS智能视频分析 http://www.betvsys.com/
- makefile中.PHNOY的用法
makefile中PHONY的重要性 伪目标是这样一个目标:它不代表一个真正的文件名,在执行make时可以指定这个目标来执行所在规则定义的命令,有时也可以将一个伪目标称为标签.伪目标通过 PHON ...
- 深度神经网络(DNN)是否模拟了人类大脑皮层结构?
原文地址:https://www.zhihu.com/question/59800121/answer/184888043 神经元 在深度学习领域,神经元是最底层的单元,如果用感知机的模型, wx + ...
- Alpha冲刺(9/10)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之9 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩准备中 和大佬们跟进进度 接下来的计划 准备答辩 ...
- MySQL 索引原理相关文章
CSDN的整理: http://bbs.csdn.net/topics/392265880 引擎在磁盘中存储顺序的图解: http://blog.csdn.net/php_lzr/article/de ...