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 ...
随机推荐
- RabbitMQ安装(一)
RabbitMQ官网 http://www.rabbitmq.com 下载地址 http://www.rabbitmq.com/download.html 一 Windows下安装RabbitMq 1 ...
- android listView多层嵌套listView显示不全问题
最近在做项目,需要用到listVIew多层嵌套listVIew的需求,先发现已下两个处理办法比较好用 第一种: public class ListViewNesting extends ListVie ...
- css模拟时钟
css模拟时钟 思路: 画时钟数字(x,y)坐标 x = x0 + r*cos(deg) y = y0 + r*sin(deg) 知识点: 创建元素: createElement 添加元素: appe ...
- k64 datasheet学习笔记31---External Bus Interface (FlexBus)
1.前言 FlexBus多功能外部总线接口控制器是一个硬件模块: (1)通过并行总线提供存储器扩展或与外设连接: (2)如下的同步或异步的slave-only设备可以直连到FlexBus总线或者只需增 ...
- windows系统下简单nodejs安装及环境配置
相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼,这里不想谈太多的nodejs的相关信息.只说一下,windows系统下简单nodejs环境配置 相信 ...
- boost.asio包装类st_asio_wrapper开发教程(一)
一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...
- strncpy的用法
strncpy是C语言的库函数之一,来自C语言标准库,定义于string.h,函数原型是: char *strncpy(char* dest,char* src,size_t n); 把src所指向的 ...
- CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS
题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...
- C++编程题
1.不用系统提供的字符串转int的功能,将一个字符串转换为对应的值 #include <iostream> using namespace std; static int StringTo ...
- Vue - Router 路由
路由的注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...