HDU 1247 - Hat’s Words - [字典树水题]
题目链接: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 - [字典树水题]的更多相关文章
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu1305 字典树水题
题意: 给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路: 字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- CDOJ 1060 秋实大哥与快餐店 字典树 水题
题目链接 B - 秋实大哥与快餐店 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- 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 ...
随机推荐
- 使用Kotlin优雅的开发Android应用
来源:https://juejin.im/post/5915c0a744d904006c4e3bcd demo下载地址:https://github.com/xiehui999/KotlinForAn ...
- killall 、kill 、pkill 命令详解 【转】
之前常用地kill 命令就是 kill -9 XXid;kill -15 XXid;pkill 进程名: 今天发现killall也有适用场景,killall命令对杀死进程组(一个进程中有多线程的情况) ...
- 【CLR】解析AppDomain
目录结构: contents structure [+] 什么是AppDomain 跨越AppDomain边界访问对象 按引用封送(Marshal-by-Reference) 按值封送(Marshal ...
- java 实现websocket
最近了解了下websocket和socket这个东西,说不得不来说下为何要使用 WebSocket ,和为何不用http. 为何需要WebSocket ? HTTP 协议是一种无状态的.无连接的.单向 ...
- 模仿CountDownLatch类自定义倒时计时器
简介 这里模仿CountDownLatch类自定义到时计时器,利用AQS模板中的尝试获得共享和释放共享 1.MyCountDownLatch package com.jacky; import com ...
- TP支持菜单动态生成RBAC权限系统数据库结构设计方案
最简单基于RBAC权限系统数据库结构设计 包括如下几个表 1. 用户表 -- Table "t_user" DDL CREATE TABLE `t_user` ( `id` int ...
- Android GIS +webservice
Android新手经典入门教程 Android开发教程(完全免费版) Android SDK v3.1.0 Android定位功能(一) Android定位功能(二) Android 百度地图开发(一 ...
- CLOS网络
CLOS网络是指为了降低多级交换网络的成本,长期以来人们一直在寻找一种交叉点数随入.出现数增长较慢的交换网络,其基本思想都是采用多个较小规模的交换单元按照某种连接方式连接起来形成多级交换网络.
- ubuntu之视频转换(Avconv的使用)
1.安装 sudo apt-get install ffmpeg libav-tools 2.基本操作 avconv [options] [[infile options] -i infile] [[ ...
- 牛客网_Go语言相关练习_选择题(2)
注:题目来源均出自牛客网. 一.选择题 Map(集合)属于Go的内置类型,不需要引入其它库即可使用. Go-Map_菜鸟教程 在函数声明中,返回的参数要么都有变量名,要么都没有. C选项函数声明语法有 ...