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 ...
随机推荐
- Lazarus中TScreen类使用介绍
描述:虚拟屏幕(桌面)可以包含多个物理显示器.Screen对象是鼠标指针.字体.窗体. 对于Delphi兼容的(不可见)DataModules也被列出了. 同时也追踪当前活动窗体窗体.控件和指针. S ...
- C#引用类型与值类型的比较
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SimpleDe ...
- Google 开源项目风格指南
Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...
- java jmx
http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ...
- Chart控件,把Y轴设置成百分比
这次所有属性设置都用代码(就当整理便于以后查询). 在窗体放置一个Chart控件,未做任何设置:然后编写代码: //设置 chart2.Legends[ ].Enabled = false;//不显示 ...
- C++ 简单中文敏感词检测工具类
具体思路: 1->敏感词库,可从数据库读取,也可以从文件加载. 2->将敏感词转化为gbk编码,因为gbk严格按照字符一个字节,汉字两个字节的格式编码,便于容易切分文字段. 3->将 ...
- C#中String 和string 的区别
C#中同时存在String与string MSDN中对string的说明: string is an alias for String in the .NET Framework.string是Str ...
- 行高不设单位的好处 line-height:1.8
今天无意间看了到了line-height:1.8 感觉挺有意思的,然后翻了下行高的知识,发现还挺有文章的,不妨温故而知新. 先回顾下:顶线.中线.基线.底线 vertical-align是元素的垂直对 ...
- jsp 标签、 项目全路径引用${CTX}
请根据自己的需要选择以下标签. <%@ taglib uri="/struts-tags" prefix="s"%><%@ taglib ur ...
- 雾里看花终隔一层——探析package和import
package是什么 package好比java用来组织文件的一种虚拟文件系统.package把源代码.java文件,.class文件和其他文件有条理的进行一个组织,以供java来使用. 源代码的要求 ...