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 ...
随机推荐
- LC 387. First Unique Character in a String
题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doesn ...
- php中连接tcp服务的三种方式
首先需要现有一个 tcp 服务,我们使用 php中的 socket 系列函数实现 <?php //创建socket套接字 $socket = socket_create(AF_INET, SOC ...
- linux tcp listen函数的参数backlog
1 listen函数(http://man7.org/linux/man-pages/man2/listen.2.html) int listen(int sockfd, int backlog); ...
- Jobs(二) Servlet的配置
折腾了一会,终于实现了:在浏览器中输入数据,然后在Java类中取出输出,并使浏览器重定向(接收一个request,返回一个response). 这里有几个问题需要详细记录一下. 首先是request的 ...
- php操作string的函数
函数库来源于:http://www.w3school.com.cn/php/php_ref_string.asp 我常用的 echo()------------输出一个或多个字符串. 如:echo ' ...
- 怎样安装并编译TypeScript?
1. 使用: npm -v 查看是否安装了 npm , 如果没有安装, 请前往 Nodejs 官网 下载安装, 下图表示已经安装 npm , 版本为: 6.9.0 . PS C:\Users\Adm ...
- Vue-img-preload
预加载页面上的图片资源,提高用户体验 效果预览 使用方法 下载vue-img-preload插件 npm install vue-img-preload 配置参数 eachLoaded(functio ...
- fragment概念理解
fragment概念理解知识,fragment概念理解图片 fragment概念理解内容,fragment概念理介绍,fragment概念理正文 Fragment是Android honeycomb ...
- h5嵌套iframe实时传参(适用vue)
今天看到一个同事研究给iframe传参,由于好奇,我自己也写了个demo,说起来其实也挺简单的,但是在此之前没有用过,便想记录一下 其中主要用到的是postMessage 在页面中引入一个iframe ...
- Linux学习(三)-Vi和Vim的区别
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道在vi里,按 u只能撤消上次命 ...