Hat’s Words(hdu1247)

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

Total Submission(s): 6156 Accepted Submission(s): 2289





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

分析:先把词典单词存入字典树,然后对每个单词进行枚举拆分,如m长度的单词要拆分m-1次,最后根据字典树判断是否拆分后的两部分单词能否都可以找到

程序:

#include"string.h"
#include"stdio.h"
#define M 50002
#include"stdlib.h"
struct st
{
int next[28];
int w;
}tree[M*5]; int index;
void creat(int k,char *ch)
{
int len=strlen(ch);
int i,s=0;
for(i=1;i<=len;i++)
{
int m=ch[i-1]-'a'+1; if(tree[s].next[m]==0)
{
if(i==len)
tree[index].w=k;
tree[s].next[m]=index++;
}
else
{
if(i==len)
tree[tree[s].next[m]].w=k;
} s=tree[s].next[m]; } }
int finde(char *ch)
{
int len=strlen(ch);
int i,s=0;
for(i=1;i<=len;i++)
{
int m=ch[i-1]-'a'+1;
if(tree[s].next[m]!=0)
{
if(i==len&&tree[tree[s].next[m]].w!=0)
return 1;
s=tree[s].next[m];
}
else
return 0;
}
return 0;
}
char ch[M][33];
int main()
{
int k=1,i,j,t;
index=1;
memset(tree,0,sizeof(tree));
while(scanf("%s",ch[k])!=EOF)
{
creat(k,ch[k]);
k++; }
char ch1[33],ch2[33];
int t1,t2;
for(i=1;i<k;i++)
{
int m=strlen(ch[i]); for(j=1;j<m;j++)
{
t1=t2=0;
for(t=0;t<j;t++)
{
ch1[t1++]=ch[i][t];
}
ch1[t1]='\0';
for(t=j;t<m;t++)
{
ch2[t2++]=ch[i][t];
}
ch2[t2]='\0';
if(finde(ch1)&&finde(ch2))
{
puts(ch[i]);
break;
}
}
}
return 0;
}

hdu1247(字典树+枚举)的更多相关文章

  1. hdu1247 字典树或者hash

    题意:      给你一些串,问你哪些串是由其他两个串连接成的. 思路:        我用了两种方法,一个是hash,hash的时候用map实现的,第二种方法是字典树,字典树我们枚举每个一字符串,查 ...

  2. hdu1247 字典树

    开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...

  3. hdu1247-Hat’s Words-(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意:给出一堆单词,求哪些单词是其中某两个单词拼接起来的. 题解:用字典树存储所有的单词,标记结束点,再次 ...

  4. HDu-1247 Hat’s Words,字典树裸模板!

    Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...

  5. HDU1247(经典字典树)

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

  6. Tire树(字典树)

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

  7. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. POJ3630Phone List(字典树)

    经典的字典树的题目了,这次完全是按照自己的风格来写的,没有参考其他人的代码风格来写. 分析:如果采用常规的暴力枚举,那么复杂度就是O(n*n*str.length) = O(10^9),这明显是会超时 ...

  9. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

随机推荐

  1. Android之Fragment学习笔记②(Fragment生命周期)

    一. Fragment生命周期图                                  二.Fragment生命周期方法介绍 Fragment的生命周期和activity生命周期很像,其生 ...

  2. 3D模型修改

    xnalara模型修改---增添(技术交流贴2) 其实很早就想做这个教程(流程)但有一种叫拖延症的东东捆了我半年~~于是这个帖子诞生与此,,希望对某些骚年有用... 送TA礼物     回复 举报|1 ...

  3. c语言作业

  4. 三种查看SqlServer中数据物理pge页的方法

    1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...

  5. php--mongodb的安装

    1.mongodb 安装 2.mongodb 扩展 http://pecl.php.net/package/mongo/1.6.14/windows

  6. 流媒体学习一-------mediastreamer2 的简介

    Mediastreamer2 是一个功能强大且小巧的流引擎,专门为音视频电话应用而开发.这个库为linphone中所有的接收.发送多媒体流提供处理,包括音/视频捕获,编码和解码,渲染. 特性: 接收. ...

  7. Cocos2d-JS轻量级开发

    官方提供了另外一种使用cocos2d js的方式,更适合web开发者,只要引用一个js就可以了 1.下载Cocos2d-JS Lite Version(去下载>>) 下载下来的将是一个完整 ...

  8. android判断pad还是手机

    第一种. Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); if (fr ...

  9. Magento多语言功能

    Magento多语言功能是建立在视图(view)基础之上的,可以通过后台创建店铺视图来创建基于Magento的多语言店铺.本文为大家讲解如何创建多语言的功能.首先,进入“管理店铺”节点,如下所示. S ...

  10. JS-002-修改元素属性(以按钮示例)

    此文以修改 button 元素属性(例如:添加属性.修改属性.修改颜色样式.边框样式等)为简单示例,演示 js 修改 HTML 元素属性的基本方法,敬请参阅.若有不足之处敬请指正,不胜感激! 多不闲述 ...