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. UI控件切圆角

    1. xib下设置View圆角 这个很简单, 只需要重写  - (void)drawRect:(CGRect)rect 方法就行了 1 2 3 4 5 6 - (void)drawRect:(CGRe ...

  2. 如何快速正确的安装 Ruby, Rails 运行环境-b

    对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境.次安装方法同样适用于产品环境! 系 ...

  3. Memcached(二)Memcached Java API基础之MemcachedClient

    1. 构造函数 public MemcachedClient(InetSocketAddress[] ia) throws IOException; public MemcachedClient(Li ...

  4. [CC150] Find a line passing the most number of points

    Problem: Given a two-dimensional graph with points on it, find a line which passes the most number o ...

  5. OA学习笔记-008-岗位管理Action层实现

    一.分析 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 2,分析有几个功能,对应几个请求. 3,实现功能: 1,写Action类,写Ac ...

  6. android java 堆栈的实现

    android和java不提供堆栈的实现,只提供了list,vector,deque得存储结构,对于以前做面向过程语言的程序员来说,总觉得缺少了些什么: Stack.java文件: public cl ...

  7. C#数据库连接字符串

    转自:http://blog.csdn.net/xiaokexinger/article/details/1541441 在MSDN中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式 ...

  8. Entity Framework 学习初级篇1--EF基本概况

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...

  9. android学习——项目的目录结构

    学习开发之前要了解工程目录下的文件夹下文件的用意,是以HelloWorld为例: 1.HelloWorld项目的目录结构 1.1.src文件夹 1.2.gen文件夹 1.3.Android 2.1文件 ...

  10. 对使命召唤OL游戏中队友能相互救治的动作设定的感慨

    很偶然的在网吧看到有人在玩一个枪战游戏,场景特别真实特别吸引人,后来留意到是使命召唤OL.我使用QQ帐号(是腾讯代理)玩了一次,觉得游戏做的确实精致,子弹打击效果和人物被子弹击中的效果特别真实,大家可 ...