题目链接: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. top命令详析及排查问题使用演示

    1. top基本使用 top命令运行图 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 buff/cache: buffers 和 cache 都是内存中存放的数 ...

  2. 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误

    Problem 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误 Resolution 这是由于SharePoint 2013中分布式缓存实例( ...

  3. 【转】java 读取 excel 2003 或 excel 2007

    package com.my.login; import java.io.File; import java.io.FileInputStream; import java.io.IOExceptio ...

  4. django admin list_filter的使用

    一.举例 class CategoryTreeRelatedFieldListFilter(admin.SimpleListFilter): title = _('课程章节') parameter_n ...

  5. AutoMapper,对象映射的简单使用

    using AutoMapper; using AutoMapper.Configuration; using System; using System.Collections.Generic; us ...

  6. 物联网架构成长之路(24)-Docker练习之Compose容器编排

    0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compo ...

  7. linux每日命令(2):cd命令

    1.命令格式: cd [目录名] 2.命令功能 切换当前目录至 [目录名] 3. 常用范例 1.进入系统根目录 命令: cd / 说明:进入系统根目录,上面命令执行完后拿ls命令看一下,当前目录已经到 ...

  8. 批处理命令学习笔记——Start命令

    Start 命令 启动另一个窗口运行指定的程序或命令,所有的DOS命令和命令行程序都可以由start命令来调用. 语法:START ["title"] [/Dpath] [/I] ...

  9. 【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用

    实验现象: 程序运行时,绿色led闪烁(目前,具体的乘法器调用请参考iCore3乘法器例程) 核心代码: module multiplier_ctrl( input clk_25m, input rs ...

  10. linux 常用命令1【转】

    1.1. 关机 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minutes & 按预定 ...