题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247

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

题意:

“帽子单词”是指,若字典中的某个词,它是由其他任意两个单词连接起来组成的,则称它为“帽子单词”。

现在以字典序给出字典中的所有单词(不超过 $5e4$ 个),让你求出全部“帽子单词”。

题解:

先把字典建成字典树,然后对于每个单词,暴力地分成两个子串查找即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+; namespace Trie
{
const int SIZE=maxn*;
int sz;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init(){sz=;}
void insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
trie[p].ed++;
}
int search(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
p=trie[p].nxt[s[i]-'a'];
if(!p) return ;
}
return trie[p].ed;
}
};
int tot;
string s[maxn];
int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie();
Trie::init();
tot=;
while(cin>>s[++tot]) Trie::insert(s[tot]);
for(int i=;i<=tot;i++)
{
bool ok=;
for(int k=;k<s[i].size();k++)
{
if(Trie::search(s[i].substr(,k)) && Trie::search(s[i].substr(k,s[i].size()-k)))
{
ok=;
break;
}
}
if(ok) cout<<s[i]<<'\n';
}
}

HDU 1247 - Hat’s Words - [字典树水题]的更多相关文章

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

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

  2. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

  3. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  4. hdu 1754 I Hate It(线段树水题)

    >>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...

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

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

  6. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  7. CDOJ 1060 秋实大哥与快餐店 字典树 水题

    题目链接 B - 秋实大哥与快餐店 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Sub ...

  8. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...

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

随机推荐

  1. 002_ASP.NET 换主题

    网盘下载地址 http://pan.baidu.com/s/1c1VzIla 在线观看地址 http://www.bamn.cn/course/lesson/2 课程介绍 ASP.NET 实现更换主题 ...

  2. 手机APP UI设计尺寸基础知识

    从原理开始介绍一下移动端设计尺寸规范 初涉移动端设计和开发的同学们,基本都会在尺寸问题上纠结好一阵子才能摸到头绪.我也花了很长时间才弄明白,感觉有必要写一篇足够通俗易懂的教程来帮助大家.从原理说起,理 ...

  3. 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx EF 6 ...

  4. Github忽略keil工程生成的链接、编译等文件

    *.bak *.ddk *.edk *.lst *.lnp *.mpf *.mpj *.obj *.omf *.plg *.rpt *.tmp *.__i *.crf *.o *.d *.axf *. ...

  5. 设置tomcat 编译文件位置【转】

    问题: 将项目发布到tomcat时,发现tomcat的cclasses目录下无任何编译后的文件. 解决方法:设置MyEclipse的文件编译目录即可: http://my.oschina.net/u/ ...

  6. 原生AJAX请求教程

    ajax 即 Asynchronous Javascript And XML,AJAX 不是一门的新的语言,而是对现有持术的综合利用.本质是在 HTTP 协议的基础上以异步的方式与服务器进行通信. 异 ...

  7. CSS让页面平滑滚动

    我们以往实现平滑滚动往往用的是jQuery, 如实现平滑回到顶部,就写如下代码: $('.js_go_to_top').click(function () { $(".js_scroll_a ...

  8. C++ 智能指针一

    /* 智能指针shared_ptr */ #include <iostream> #include <string> #include <memory> //智能指 ...

  9. Python终端自动补全

    在-目录下添加一个文件,名字为.pythonstartup.py #!/usr/bin/python # -*- coding: UTF-8 -*- import readline, rlcomple ...

  10. js实现滑动的弹性导航

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...