hdu1247(字典树+枚举)
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(字典树+枚举)的更多相关文章
- hdu1247 字典树或者hash
题意: 给你一些串,问你哪些串是由其他两个串连接成的. 思路: 我用了两种方法,一个是hash,hash的时候用map实现的,第二种方法是字典树,字典树我们枚举每个一字符串,查 ...
- hdu1247 字典树
开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...
- hdu1247-Hat’s Words-(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意:给出一堆单词,求哪些单词是其中某两个单词拼接起来的. 题解:用字典树存储所有的单词,标记结束点,再次 ...
- HDu-1247 Hat’s Words,字典树裸模板!
Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...
- HDU1247(经典字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Tire树(字典树)
from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- POJ3630Phone List(字典树)
经典的字典树的题目了,这次完全是按照自己的风格来写的,没有参考其他人的代码风格来写. 分析:如果采用常规的暴力枚举,那么复杂度就是O(n*n*str.length) = O(10^9),这明显是会超时 ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
随机推荐
- PowerDesigner连接MySQL,建立逆向工程图解
传说中,程序员们喜欢用powerDesign进行数据库建模.通常都是先设计出物理模型图,在转换出数据库需要的SQL语句,从而生成数据库.但,江湖中流传着"powerDesign逆向工程&qu ...
- jira attachement directorey,workflow---extention.
workflow---extention. https://confluence.atlassian.com/jirakb/jira-miscellaneous-workflow-extensions ...
- 【Android开发学习笔记】【第九课】重力感应
概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...
- Linux Socket过程详细解释(包括三次握手建立连接,四次握手断开连接)
我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web 服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠s ...
- android Edittext自定义输入字符和类型
android提供了两种方式 一种是setInputType 一种是setKeyListener 直接上代码,有注释 import android.app.Activity; import andro ...
- Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)
通过 JS 或 JQuery 获取到元素后,通过 offsetLeft.offsetTop.offsetWidth.offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参 ...
- Windows-006-映射网络驱动器图文详解
此文主要讲述 Win7 中,如何映射网络驱动器,一般用于网络共享时.敬请亲们参阅,若有不足之处,敬请大神指正,不胜感激! 打开计算机,选择工具栏中的 映射网络驱动器,依据下图中的操作进行映射网络驱动器 ...
- synchronized锁自旋
http://www.jianshu.com/p/5dbb07c8d5d5 原理 通常说的synchronized在方法或块上加锁,这里的锁就是对象锁(当然也可以在类上面),或者叫重量锁,在JVM中又 ...
- http://www.cnblogs.com/littlemonk/p/5705476.html
http://www.cnblogs.com/littlemonk/p/5705476.html
- shell脚本编程-处理用户输入
命令行参数 命令行参数:允许在运行脚本时向命令行添加数据值 如:$ ./addem 10 30 读取参数 bash shell会将一些称为位置参数的特殊变量分配给命令行输入的所有参数,甚至包括shel ...