Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9574    Accepted Submission(s): 3421

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

题意:推断一个单词能不能有两个单词组成,能够的话就输出。

题解:全部单词建成一颗字典树,单词插入结尾记录单词的个数。查询时直接暴力拆单词推断。

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstdlib> #define N 50020 using namespace std; char s[N][42]; struct Trie {
int num;
struct Trie *nxt[26];
Trie() {
num=0;
for(int i=0; i<26; i++) {
nxt[i]=NULL;
}
}
}; void Trie_Inser(Trie *p,char s[]) {
int i=0;
Trie *q=p;
while(s[i]) {
int nx=s[i]-'a';
if(q->nxt[nx]==NULL) {
q->nxt[nx]=new Trie;
}
i++;
q=q->nxt[nx];
}
q->num+=1;
} bool Trie_Serch(Trie *p,char s[],int be,int en) {
Trie *q=p;
int i=be;
while(i<=en) {
int nx=s[i]-'a';
if(q->nxt[nx]==NULL)return false;
q=q->nxt[nx];
i++;
}
if(q->num>0)return true;
return 0;
} int main() {
//freopen("test.in","r",stdin);
Trie *p=new Trie;
int id=1;
while(~scanf("%s",s[id])) {
Trie_Inser(p,s[id]);
id++;
}
for(int i=1; i<id; i++) {
int len=strlen(s[i]);
int be=0;
if(len<=1)continue;
for(int en=0; en<len-1; en++) {
if(Trie_Serch(p,s[i],be,en)&&Trie_Serch(p,s[i],en+1,len-1)) {
printf("%s\n",s[i]);
break;
}
}
}
return 0;
}

hdu 1247 Hat’s Words(字典树)的更多相关文章

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

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

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

  3. Hdu 1247 Hat's Words(Trie树)

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

  4. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  6. hdu 1247:Hat’s Words(字典树,经典题)

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

  7. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  8. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...

  9. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. log的6种等级

    在Java中,log有6种等级,从低到高为: (1)TRACE:用于展现程序执行的轨迹 (2)DEBUG:用于协助低层次的调试 (3)INFO:用于基本高层次的诊断信息,在长时间运行的代码段开始运行及 ...

  2. Java入门-浅析Java学习从入门到精通【转】

    一. JDK (Java Development Kit)  JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库 ...

  3. 安装app到Simulator

    1.安装brew 打开命令行,执行以下命令: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install ...

  4. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    Description Input * Line 1: 牛的数量 N. * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度. Output * Line 1: 一个整数表示c[ ...

  5. 二维卷积c代码

    二维卷积c代码 二维信号的卷积原理请参考另外一篇文章:http://blog.csdn.net/carson2005/article/details/43702241 这里直接给出参考代码: void ...

  6. HTML5安全:CORS(跨域资源共享)简介

    前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...

  7. 【Tools】Pro Git 一二章读书笔记

    记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧.   Pro Git (Scott Chacon) 读书笔记:   ...

  8. zabbix 通过key 获取

    zabbix:/root# zabbix_get -s 192.168.2.224 -k "perf_counter[\Processor(_Total)\% Processor Time] ...

  9. oracle core 概述

    oracle数据库系统的架构及其复杂,其提供的特性也非常的多.作为一种关系型数据库,oracle提供的基本特性: transaction concurrency read consistent 而支撑 ...

  10. suse系统卸载数据库实例

    1.停止数据库: 2.执行以下命令: find $ORACLE_BASE/* -name '*[Tt][Ee][Ss][Tt]*'  其中TEST为数据库的实例名: 删除存在的文件: 3.删除/etc ...