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 ...
随机推荐
- Java 判断字符是大写小写或者数字
使用character类 Character.isLowerCase(Schar.charAt(i)) //获取字符串Schar中的某一个字符然后借用character类的方法来判断是不是小写. 其他 ...
- STM32之DMA实例
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zouleideboke/article/details/75092558 DMA简介: DMA(Di ...
- VSFTP添加用户
VSFTPD的安装网上有很多教程这里就不多说了,这里主要是针对做主机空间服务的朋友在安装好vsftpd后如何为用户增加ftp账号 先来看一看我们一般在*inux系统下面如何增加用户的 #adduser ...
- Get HttpWebResponse and HttpClient Return String by proxy
#region Get HttpClient Return String /// <summary> /// Get HttpClient Return String /// </s ...
- 12-MySQL DBA笔记-MySQL复制
第12章 MySQL复制 本章将为读者讲述MySQL的复制技术,首先,介绍最基础的主从复制,它是其他所有复制技术的基础,接着再为读者讲述各种复制架构的搭建,最后,列举了一些常见的复制问题及处理方式.复 ...
- 图解Java继承内存分配
图解Java继承内存分配 继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子 ...
- SQLException: #22001你知道这个错误码吗
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! java.sql.SQLException: #22001 java.sql.SQLException: #22001 a ...
- 排查RabbitMQ安装错误
1.注册表中是否有 HKEY_LOCAL_MACHINE\SOFTWARE\Ericsson\Erlang\ErlSrv\1.1\RabbitMQ 此项.(须有) 2.安装目录是否存在中文.(不可有 ...
- 处理python错误问题
------------恢复内容开始------------ 调试过程中遇到的问题 (1)爬取首页源码出现中文乱码 解决方案: 将网页编码强制转换成gbk,并去除解决乱码问题的三行代码. (2)程序运 ...
- js重点——作用域——简单介绍(一)
一.作用域 定义:在js中,作用域为变量,对象,函数可访问的一个范围. 分类:全局作用域和局部作用域 全局作用域:全局代表了整个文档document,变量或者函数在函数外面声明,那它的就是全局变量和全 ...