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

题意:给出多个单词,要在这些单词中找出正好是这多个单词中另外两个单词首尾相连构成的,并按字典序输出所有找到的单词

思路:首先利用给出的单词构建字典树,然后遍历每个单词,将单词分成两段,看这两段是不是都在 Trie 中,若在则直接输出

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define E 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=7;
const int N=100000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;int tot;
int trie[N][50];//trie[rt][x]=tot,root是上个节点编号,x是字母,tot是下个节点编号
bool vis[N];//查询整个单词用
void insert(char *s,int root){for(int i=0;s[i];i++){int x=s[i]-'a';if(trie[root][x]==0)//现在插入的字母在之前同一节点处未出现过trie[root][x]=++tot;//字母插入一个新的位置,否则不做处理root=trie[root][x];//为下个字母的插入做准备}vis[root]=true;//标志该单词末位字母的尾结点,在查询整个单词时用
}
bool find(char *s,int root){for(int i=0;s[i];i++){int x=s[i]-'a';if(trie[root][x]==0)return false;//以rt为头结点的x字母不存在,返回0root=trie[root][x];//为查询下个字母做准备}return vis[root];//查询整个单词时
}
char word[N][50];
int main(){int cnt=0;tot=1;while(scanf("%s",word[cnt])!=EOF)insert(word[cnt++],1);for(int i=0;i<cnt;i++){//枚举所有单词int len=strlen(word[i]);for(int j=1;j<len;j++){//拆分当前单词char str1[50],str2[50];strncpy(str1,word[i],j);str1[j]=0;strncpy(str2,word[i]+j,len-j);str2[len-j]=0;if(find(str1,1) && find(str2,1)){printf("%s\n",word[i]);break;}}}return 0;
}

Hat’s Words(HDU-1247)相关推荐

  1. 有源汇有上下界最大流/最小流 配题(HDU 3157)

    因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...

  2. 最大表示法--环形字符串最大字典序(HDU 5442)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 问题概述:n个字符围成一个环,请从这个环中找出字典序最大的长度为n的字符串,输出它的起始点和方向(0顺1 ...

  3. HDU2019多校第二场 1009(HDU 6599) I Love Palindrome String(回文树(自动机)+manacher)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 解题思路: 回文自动机求每个本质不同的子串出现的次数,同时记录每个节点i代表的回文串第一次出现的 ...

  4. S-Nim (HDU 1536)组合博弈SG多组游戏

    S-Nim 题目链接 Problem Description Arthur and his sister Caroll have been playing a game called Nim for ...

  5. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  6. (HDU - 1847)Good Luck in CET-4 Everybody!(博弈)

    题目链接:Good Luck in CET-4 Everybody! - HDU 1847 - Virtual Judge (ppsucxtt.cn) 题目是中文的,我在这就不翻译题意了. 先说一种打 ...

  7. 美素数(HDU 4548)(打表,简化时间复杂度)

    相信大家都喜欢美的东西,让我们一起来看看美素数吧. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11 ...

  8. 单词数(HDU 2072)

    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...

  9. Ant Trip(HDU 3018)---多笔画问题

    题目链接 题目描述 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together w ...

  10. Virtualbox 安装Red hat Enterprise Linux(64位)

    1.需要安装 Virtualbox. 2.新建一个虚拟机. 3,为虚拟机命名(选择Linux,RedHat64位的版本)可能有的电脑选不到64位的版本,只有32位的版本,接下来会说明. 如果没有显示6 ...

最新文章

  1. 《SolidWorks 2016中文版机械设计从入门到精通》——第1章 认识SolidWorks1.1 SolidWorks概述...
  2. 软考 中级职称哪些最热门_我如何利用有史以来最热门的中级故事来建立排行榜。 以及它几乎是怎么死的。...
  3. (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
  4. 张一鸣批评的互联网“语言腐败”,危害到底有多大?
  5. 自动化测试 短信验证登录
  6. 抢票软件开发(二) 模拟登录
  7. linux电子表格工具,Linux系统办公一条龙之电子表格Calc
  8. java使用poi导出excel 包括多个工作簿
  9. UE4设置人物移动和人物视角
  10. 对自定义View的Measure和onMeasure的一点心得
  11. 一个古典App开发者的DApp开发之路
  12. 第一章:python入门储备知识
  13. [转]NLP关键词提取方法总结及实现
  14. 微信小程序 基本认识
  15. can't find lxxx解决办法
  16. 项目管理工具——项目开发者工具
  17. 教育行业电商SaaS系统解决方案:助力企业实现经营、管理一体化
  18. useradd -g -G
  19. 快乐AK场2 E 删删删越小越好 单调栈
  20. Motivated Word(6)

热门文章

  1. 用if语句表达区间分支
  2. Github 上热门的 Spring Boot 项目实战推荐
  3. 修改Yarn的全局安装和缓存位置
  4. Minidao_1.6.1版本发布,超轻量Java持久化框架
  5. Description Resource Path Location Type Java compiler level does not match the version of the insta
  6. 自动化集成:Pipeline整合Docker容器
  7. 数据仓库组件:Hive环境搭建和基础用法
  8. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
  9. 图像领域深度学习的七个境界
  10. WinDbg 命令三部曲:(二)WinDbg SOS 扩展命令手