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. oracle监听器初识-配置多SERVICE_NAMES

    现象: 为数据库设置多个服务名(通过SCOPE=both设置,同时修改参数文件) SQL> show parameter service_names; NAME TYPE VALUE ----- ...

  2. 【转】Python 面向对象(初级篇)

    [转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...

  3. *445. Add Two Numbers II

    1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...

  4. dubbo源码分析10——服务暴露1_export()方法分析

    ServiceConfig类中的export()方法,是dubbo服务暴露的入口方法,被触发的时机有两个: 1. spring容器初始化完成所有的bean实例后,通过事件机制触发 2. 实现Initi ...

  5. Linux电源管理(7)_Wakeup events framework【转】

    转自:http://www.wowotech.net/pm_subsystem/wakeup_events_framework.html 1.  前言 本文继续“Linux电源管理(6)_Generi ...

  6. 解决ubuntu的gedit编辑器中文乱码的问题

    hello,本人 sky 又和大家见面了很多人在使用ubuntu系统时发现打开windows系统下面写的文档的话会发现乱码,是因为编码格式的问题windows系统下面是用GB2312等编码格式进行中文 ...

  7. Nginx在线服务状态下平滑升级及ab压力测试【转】

    今天,产品那边发来需求,说有个 APP 的 IOS 版本下载包需要新增 https 协议,在景安购买了免费的 SSL 证书.当我往 nginx 上新增 ssl 时,发现服务器上的 nginx 居然没编 ...

  8. python练习实例

    #!/usr/bin/python # -*- coding: UTF-8 -*- try: fh = open("testfile","w") fh.writ ...

  9. [转载]RabbitMQ消息可靠性分析

    有很多人问过我这么一类问题:RabbitMQ如何确保消息可靠?很多时候,笔者的回答都是:说来话长的事情何来长话短说.的确,要确保消息可靠不只是单单几句就能够叙述明白的,包括Kafka也是如此.可靠并不 ...

  10. HDU 5033

    题意: 给你 N 楼房, 然后给你m个人站在这些楼房之间, 问看到天空的仰角是多少度 思路: 对于每一个人, 算出左边的凸包, 和右边的凸包, 找出最大斜率点, 算角度即可 (我在线算比较费时, 离线 ...