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树)相关推荐

  1. HDU 1247 Hat’s Words 字典树(Trie树)

    HDU 1247 Hat's Words 字典树(Trie树) 字典树的建立是应该都是一样的 下面是我的做法: 建立完后, 对每一个单词都进行find_string()判断是否符合, 分别对其分成两半 ...

  2. HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址 : http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目描述: ...

  3. HDU 1247(Hat’s Words )

    /* Date: 2012/10/27 题目链接地址:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路:把字典中的每个字符串拆成0->j 和 j ...

  4. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  5. HDU 1247 Hat’s Words

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. hdu 1251 统计难题(字典树)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 分析: 关于字典树的题目似乎都有一个普通适用性的模板,有时只是稍加改动来满足临时的要求,我的一 ...

  7. HDU - 4757 Tree(LCA+可持久化trie树)

    题目链接:点击查看 题目大意:给出一棵有有n个节点的树,每个点都有一个权值,现在给出m个查询,每次查询的形式为:x,y,z,求出从点x到点y的路径上任选一点,使其与z的异或值最大,输出异或值 题目分析 ...

  8. hdu 1251 统计难题 (Trie树)

    本题是trie树模板题,如果不用trie而用map写可以看出trie处理这类问题有明显的时间优势. 在trie树中查找一个关键字的时间和树中包含的结点数无关,而取决于组成关键字的字符数.(对比:二叉查 ...

  9. 【trie树】HDU1247Hat’s Words

    Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

最新文章

  1. java项目校内网的描述_[导入]校内网的错误信息
  2. python嵌套循环跳出_如何跳出嵌套的while循环
  3. 非关系型数据库(非RDBMS)介绍
  4. ppp在linux下的编译和安装
  5. ajax工作中使用模板
  6. 《团队激励与沟通》第 5 讲——沟通的技巧 重点部分总结
  7. android+残留软件包名,关于使用pm hide伪卸载系统软件的一些包名
  8. python mysql到处excel
  9. 语义slam_【语义SLAM】MIT 新开源语义VIOSLAM框架 kimera
  10. 七月算法机器学习1 相关数学基础
  11. debug工具_Hackintool for【黑】mac V3.4.4中文版 黑苹果必装工具箱
  12. C++ 实现CRC循环冗余校验码
  13. linux drm 写屏幕,Linux DRM Graphic 显示简单介绍
  14. exec与sp_executesql
  15. 工欲善其事之Redis可视化管理工具
  16. 什么是跨域?如何解决跨域?
  17. 数仓工具—Hive源码之cli 模块(2)
  18. win10系统D盘出现莫名其妙的占用
  19. 服务器证书已过期,WebSphere应用服务器证书过期问题解决
  20. 【学习教程】全系统各类型工程水土保持方案编制实践技术系列

热门文章

  1. 物联网发展的十个预言
  2. 手机屏幕分辨率规格尺寸全解析
  3. 二阶振荡环节的谐振频率_2个惯性环节相乘=1个全部阻尼范围下的二阶振荡环节?...
  4. Allegro 172版本自动放置层叠
  5. Windows版redis下载 与 RDM下载
  6. python实用教程答案 郑阿奇_《》 mysql实用教程郑阿奇实验报告答案
  7. 大型机学习之初步了解-什么是大型机
  8. 认识GPL/LGPL
  9. 域格9x07模块电信物联网卡(单4G卡)处理方法
  10. 2021-07-29 Ubantu18.04下Cura编译