Hdu 1247 Hat's Words(Trie树)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14083 Accepted Submission(s): 5049
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
Author
戴帽子的
Recommend
Ignatius.L
/*
这题询问Trie树上是否存在一个单词,
满足拆成两部分,使得两部分单词都在树上.
然后其实直接从树上找一个单词
把是单词结尾的位置压栈
然后暴力枚举断点检验即可.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 300001
#define MAXM 50001
using namespace std;
int tot;
char s[MAXM][27];
struct data{int next[27];bool b;bool w;}
tree[MAXN];
void Add_tree(int l,char s[])
{
int now=0;
for(int i=0;i<l;i++)
{
int x=s[i]-96;
if(tree[now].next[x]) now=tree[now].next[x];
else tot++,tree[now].next[x]=tot,now=tot;
}
tree[now].b=true;
}
bool jd(int l,char s[])
{
int i=0,top=0,stack[1001],now=0;
while(s[i])
{
if(tree[now].next[s[i]-96]) now=tree[now].next[s[i]-96];
else return 0;
if(tree[now].b&&s[i])//所谓割点
stack[top++]=i+1;
i++;
}
while(top)//检验
{
int now=0;
bool flag=1;
int x=stack[--top];
while(s[x])
{
if(!tree[now].next[s[x]-96]){flag=false;break;}
now=tree[now].next[s[x]-96];
x++;
}
if(tree[now].b&&flag)//该结点是单词的结尾
return 1;
}
return 0;
}
int main()
{
int i=1;
while(gets(s[i])&&strlen(s[i]))
{
int l=strlen(s[i]);
Add_tree(l,s[i]);
i++;
}
for(int j=1;j<=i;j++)
{
int l=strlen(s[j]);
if(jd(l,s[j]))
cout<<s[j]<<endl;
}
return 0;
}
Hdu 1247 Hat's Words(Trie树)的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- HDU1247 - Hat’s Words(Trie树)
题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- Earth Wind and Fire CodeForces - 1148E (构造)
大意: $n$个石子, 第$i$个石子初始位置$s_i$, 每次操作选两个石子$i,j$, 要求$s_i<s_j$, 任取$d$, 满足$0\le 2d\le s_j-s_i$, 将$s_i,s ...
- hdu 2767 强连通缩点处理加边问题
#include <cstring> #include <cstdlib> #include <cstdio> 缩点的好处就是可以将乱七八糟的有向图 转化为无环的有 ...
- UML学习(四)-----状态图
状态图主要用于描述对象具有的各种状态.状态之间的转换过程以及触发状态转换的各种事件和条件. 1.状态图的组成 1.1 状态 主要用于描述一个对象在生命周期内的一个时间段.状态图中的状态包括状态名.内部 ...
- Css解决表格超出部分用省略号显示
小伙伴们有没有的遇到页面显示时,因为数据太长导致显示的表格某一列过长,从而导致页面的不美观,下面我们来看一看如何用Css样式解决表格超出部分用省略号显示的问题. 主要设置两个样式: table{ ta ...
- Abp 聚合测试
Abp 官网开始的教程例子,是IRpositoty<entity> 直接出现在应用层.但是如果是一个聚合根也会这样吗? 那么聚合根是访问仓储的最小单元,要通过聚合根来操作业务,就是实体, ...
- JDBC 插入时间字段的值
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
- JAVA对ArrayList排序
ava如何对ArrayList中对象按照该对象某属性排序 增加排序功能,打印时:输出学生对象的时候,需要先按照年龄排序,如果年龄相同,则按照姓名排序,如果姓名也相同,则按照学号排序. Code hig ...
- 8.SpringMVC注解式开发-HelloWorld
第一个注解式开发程序 SpringMVC 是 Spring 框架的一部分,所以它和Spring结合的是非常紧密的 使用 @Controller 注解,既可以将处理器交给Spring容器去管理,又可以说 ...
- 把app(apk和ipa文件)安装包放在服务器上供用户下方法
怎么把app(apk和ipa文件)安装包放在服务器上供用户下载? IIS服务器网站不能下载.apk文件的原因:IIS的默认MIME类型中没有.apk文件,所以无法下载.解决办法:给.apk格式文件添加 ...
- 【python】导入自定义模块
一.直接import 1.当执行文件与要导入的py文件在同一目录下时 假设要在wangyi.py中导入weibo.py文件 import weibo 2.当执行文件与要导入的py文件所在文件夹在同一目 ...