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 ...
随机推荐
- Deep_learning
https://en.wikipedia.org/wiki/Deep_learning
- Map的数据结构
一:Map<String,Map<String,Map<String,List<A>>>>
- SpringMVC 基于注解的Controller详解
本文出处 http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spri ...
- Windows与Linux共享文件夹互相访问
[原文] 首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...
- oracle EBS 资产定义
一.资产定义也就是江项目任务上的特定(能生成资产的)物料按照一定格式生成资产信息,其中每个独立物料生成一条资产,具体操作步骤如下: 1.省本部库存超级用户系统内生成领料单.审批领料单.最后进行出库处理 ...
- Win7下VS2008编译QtiPlot
Win7下VS2008编译QtiPlot By wangsh 2011-11-25 QtiPlot 是一款开源免费的科学绘图软件,可运行在多个平台(windows.Linux等)中,从功能上讲,Qti ...
- 判断webkit中的js引擎是否是v8
<html><head></head><body><script type="text/javascript"> if ...
- Xcode 自定义代码段
看见老师敲程序时,快捷键一打,所需要的一整行代码都出来了,着实感觉到效率太高了. 看了几天,才反应过来为什么自己没有get这个方法呢,现在就整理一番,记录一下. 此处以@property(nonato ...
- 5分钟弄懂Docker!
http://www.csdn.net/article/2014-07-02/2820497-what%27s-docker 关注点:1.DOCKER和VM的架构区别 2.Docker 的容器利用了 ...
- 监控mysql主从同步状态脚本
监控mysql主从同步状态脚本 示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroo ...