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. IBM 3650 M3 yum upgrade后系统无法登陆问题

    一.背景 IBM 3650 M3安装了centos7.2操作系统 今天yum upgrade升级centos7.6,重启系统后发现开不了机,报错如下: Failed to set MokListRT: ...

  2. quartz定时任务的简单使用

    开发环境: springmvc quartz-2.2.3在官网下载的. 步骤: 1.首先在web.xml中加入以下代码: <servlet> <servlet-name>Qua ...

  3. 记一次手动SQL注入

    1.检测到可能存在注入漏洞的url 最常用的 ' ,and 1=1 ,and 1=2 http://www.xxx.com/subcat.php?id=1 2.判断字段个数 http://www.xx ...

  4. 引入第三方SDK allowBackup value不一致引起的编译异常

    项目中要引入一个客服的SDK,项目中 <application android:name=".AppApplication" android:allowBackup=&quo ...

  5. Android JS 交互出现 Uncaught Error: Error calling method on NPObject

    由于HTML5的功能越来越强大,native app的一些功能逐步被html页面代替,不可避免的JS交互也用到的也越来越多.在第一个版本向第二个版本迭代的过程中却发生了莫名其妙的问题,第一个版本JS调 ...

  6. 2018 Multi-University Training Contest 2 杭电多校第二场

    开始逐渐习惯被多校虐orz  菜是原罪 1004  Game    (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...

  7. 反卷积(deconvolution)

    deconvolution讲解论文链接:https://arxiv.org/abs/1609.07009 关于conv和deconvoluton的另一个讲解链接:http://deeplearning ...

  8. Shell-求平均

    Compute the Average Given integers, compute their average correct to three decimal places. Input For ...

  9. Linker Scripts3--链接脚本概述

    1.前言 本文主要翻译了The Link Script英文文献. (1)每个链接都是由链接脚本控制,链接脚本是用链接命令语言写的: (2)链接脚本的主要目的是描述输入文件的sections如何映射到输 ...

  10. ios webview调试

    chrome://inspect/#devices if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) { WebView.setWe ...