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(字典树活用)的更多相关文章

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

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

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

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

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

  4. 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 ...

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

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

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

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

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

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

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

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

  10. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. springboot+layui实现PC端用户的增删改查 & 整合mui实现app端的自动登录和用户的上拉加载 & HBuilder打包app并在手机端下载安装

    springboot整合web开发的各个组件在前面已经有详细的介绍,下面是用springboot整合layui实现了基本的增删改查. 同时在学习mui开发app,也就用mui实现了一个简单的自动登录和 ...

  2. 最近邻算法(KNN)

    最近邻算法: 1.什么是最近邻是什么? kNN算法全程是k-最近邻算法(k-Nearest Neighbor) kNN算法的核心思想是如果一个样本在特征空间中的k个最相邻的样本中的大多数数以一个类型别 ...

  3. X86架构

    在接触BIOS的时候,都需要对PC架构有一定的认知.目前的PC架构绝大多数都是Intel的X86架构,貌似也是因为INTEL的这个X86架构早就了目前INTEL如日中天的地位. 废话不多说,X86架构 ...

  4. C/C++经典面试题一

    1.变量的声明和定义有什么区别? 常量:在程序执行过程中,不会发生改变的量,不能被改变的量 变量:在程序执行过程中,可以被改变的量 定义变量的方式:数据类型 变量名 = 常量: int num = 1 ...

  5. workqueue --最清晰的讲解

    带你入门: 1.INIT_WORK(struct work_struct *work, void (*function)(void *), void *data) 上面一句只是定义了work和work ...

  6. 使用C++编写linux多线程程序

    前言 在这个多核时代,如何充分利用每个 CPU 内核是一个绕不开的话题,从需要为成千上万的用户同时提供服务的服务端应用程序,到需要同时打开十几个页面,每个页面都有几十上百个链接的 web 浏览器应用程 ...

  7. IEnumerable和IEnumerator使用

    IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象. IEnumerator是一个真正的集合访问器,没 ...

  8. python操作三大主流数据库(13)python操作redis之新闻项目实战①新闻数据的导入

    1.新闻处理页面redis_news.py #coding:utf-8 import math import redis class RedisNews(object): def __init__(s ...

  9. nginx+ssl 服务器 双向认证

    项目后台服务器采用nginx+tomcat 负载均衡架构  不久 访问协议有http升级为https 对服务器认证采用沃通的ssl证书 nginx ssl证书安装 参照沃通官方文档 他们有技术支持沟通 ...

  10. Javascript杂!

    JavaScript 标准参考教程(alpha) javascript中的 Object.defineProperty()和defineProperties JS压缩混淆  ---- 雅虎YUI 在线 ...