Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20441    Accepted Submission(s): 7162

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
题意:只有一个测试样例,输入到文件结束。从输入的单词中,找到一个单词是由两个输入的其他单词组成的单词,找出所有符合条件的单词。
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
char s[400005][26];
int vis[400005],tree[400005][26];
int n=0,num=0,root,id,len;
void insert()
{len=strlen(s[n]);root=0;for(int i=0;i<len;i++){id=s[n][i]-'a';if(!tree[root][id])tree[root][id]=++num;//注意是++numroot=tree[root][id];}vis[root]=1;//每个单词结束标记一下
}
int search(char *ss)
{len=strlen(ss);root=0;for(int i=0;i<len;i++){id=ss[i]-'a';if(tree[root][id])root=tree[root][id];if(vis[root]==1){int root2=0;//根节点继续从0开始for(int j=i+1;j<len;j++){int id2=ss[j]-'a';if(!tree[root2][id2])break;root2=tree[root2][id2];if(vis[root2]==1&&j==len-1)return 1;}}}return 0;
}
int main()
{while(~scanf("%s",&s[++n])){insert();}for(int i=1;i<=n;i++){if(search(s[i]))printf("%s\n",s[i]);}return 0;
}

转载于:https://www.cnblogs.com/-citywall123/p/11138940.html

hdu1247 Hat’s Words相关推荐

  1. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

  2. hdu1247(Hat’s Words)

    我以为像a.aa这样的输入应该是没有输出的,结果还是要输出aa. 建树的时候就是常规建树,不过查找的时候要做一些变形:对于一个单词,从第一位检查有没有单词是它的前缀,如果有的话,再去检查它的后半部分是 ...

  3. 题目梳理(一)(2019.07.06~2019.07.20)

    一.字典树类型 A   HihoCoder1014 Trie树(已做) 题目链接    https://hihocoder.com/problemset/problem/1014 B    HDU12 ...

  4. HDU1247 字典树 Hat’s Words(Tire Tree)

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

  5. Hat’s Words(HDU-1247)

    Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...

  6. 【Trie】【HDU1247】【Hat’s Wordsfd2】

    题目大意: hat's word 的定义是字典中 恰好由另外两个单词连接起来的单词 给你一本字典,问有多少个hat's word,(字典按字典序给出) 单词数50000.. 初步思路: 单词分为前缀单 ...

  7. Hat’s Words(字典树)

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

  8. Red Hat Linux 安装教程

    一.下载链接 链接:https://pan.baidu.com/s/1JShQmOrgGG5_uaqPUuaHLg  提取码:ture 二.安装步骤 1.打开虚拟机,单击"创建新的虚拟机&q ...

  9. 在Red Hat Linux5下构建LAMP网站服务平台之MySQL、PHP的安装与配置

    在Red Hat Linux5下构建LAMP网站服务平台之MySQL.PHP的安装与配置 2010-09-09 16:40:49 标签:PHP Linux mysql RedHat [推送到技术圈] ...

最新文章

  1. 【组队学习】【26期】编程实践(Python办公自动化)
  2. android拍照功能无预览,Android 无预览拍照
  3. FastJson的使用方法总结
  4. nginx应用总结(1)--基础认识和应用配置
  5. C++ map中使用erase应该注意到的问题
  6. 使用Git将本地文件夹同步至github
  7. 基于软件界面的汽车故障模拟系统
  8. PDMS.NET窗体
  9. mro列表_类的继承mro
  10. echarts中的x轴y轴颜色,文字颜色改变
  11. 用英语说计算机的优点1000,我的英语老师作文1000字
  12. 中文乱码问题—字符集utf8、uf8mb4与排序规则
  13. 电脑基础:键盘F1~F12你会用吗?天天都看到,但会用的真没几个!
  14. 如何树立正确的工作态度
  15. JS:验证、限制纯数字或者英尺英寸格式的值
  16. su 和 sudo su 的区别
  17. 科技爱好者周刊(第 213 期):知识孤岛,知识软件
  18. 相机拍出来的图片有“水波纹”的原因
  19. python怎么调字体_python怎么改字体
  20. 年终小结:什么是智能?

热门文章

  1. vue 实现表格循环滚动 vue-seamless-scroll插件的安装与使用
  2. 【java笔记】类的抽象
  3. 【动态规划】线性dp P1043 数字游戏
  4. 【数据结构和算法笔记】图的相关概念(有向图,无向图......)
  5. FFmpeg源代码简单分析:avformat_write_header()
  6. matlab输出n个a,输入a,n两个参数,要求输出aaa...a(共n个)的值
  7. tp5连接mongo和mysql_tp5(thinkPHP5)操作mongoDB数据库的方法
  8. HashMap遍历方式
  9. npm 安装出错 npm ERR! request to https://registry.npmjs.org/express failed, reason: unable to verify th
  10. C语言 main函数参数 argc,argv 用处,控制台应用程序模板2