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. php 请求参数限制

    公司有个群发短信的小项目,项目上线了很久也没有什么问题,最近有商家说 我短信群发不能用 现象是:发现有时候可以发送,有时候不可以发送,看截图发送的手机数量不一样 通过调试php代码发现 php 只接受 ...

  2. PHP正则表达式;数组:for()遍历、 foreach ()遍历、each()list()组合遍历;指针遍历

    正则表达式:    1.定界符号        任何字符,一般用  //    2. 模式修正符i        写在定界符外面后面,可不区分大小写    3.preg_replace($reg,&q ...

  3. 蓝牙Host Controller Interface笔记

    1.概述     HCI提供了一个统一的使用蓝牙控制器(BR/EDR Controller,BR/EDR/LE Controller,LE Controller,AMP Controller等)的方法 ...

  4. 关于YARN的HA

    一:准备 1.规划 namenode               namenode ZKFC ZKFC journalnode        journalnode               jou ...

  5. .net加载到vb 进程

    .net加载到vb 进程时,总是不能加载进去,什么原因呢? 要尝试三个步骤, 首先调试vb ,没有问题,代码能够调试 然后注册.net的dll,生成tlb文件,生成解决方案,调整附加到进程时的选项. ...

  6. js跳转方法

    很有用的东西,参考自http://www.zhuoda.org/lunzi/66097.html 第一种 <script language="javascript" type ...

  7. JAVA并发编程的艺术

    CAS有两个特点: 1.for循环 2.compareAndSet(可能别的线程先改变然后又重置,此时CAS是成功的,也就是CAS执行的过程中,可能多个线程对此变量做了修改,而不是各个线程互斥的修改) ...

  8. 为 UIButton 添加长按事件

    UIButton *aBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 6 ...

  9. Java Convert String & Int

    To convert a int to string: int num = 123; String str = String.valueOf(num); To convert a string to ...

  10. c语言之内存的申请malloc() 和释放free()

    c语言之内存的申请malloc() 和释放free() 1.如何使用 malloc 函数 malloc是一个函数,专门用来从堆上分配内存.使用malloc函数需要几个要求: 内存分配给谁?分配多大内存 ...