分析:一開始是用递归做的,没做出来。于是就换了如今的数组。即,把每个输入的字符串都存入二维数组中,然后创建字典树。输入和创建完成后,開始查找。

事实上一開始就读错题目了,题目要求字符串是由其它两个输入的字符串组成的前后缀,自己根本没有推断前缀是否满足。就直接推断后缀,一直想不通自己哪里错了,非常羞愧,水平还是不行。

查找分为前缀查找和后缀查找。事实上两个函数基本差点儿相同的。以下放代码。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; struct trie
{
trie *next[26];
int v; //字符同样的个数
trie()
{
memset(next,NULL,sizeof(next));
v=1;
}
}; trie *root=new trie();
char s[50001][101]; void creat_trie(char *str)
{
int i,id;
trie *p;
for(p = root,i=0;str[i]; ++i)
{
id = str[i]-'a';
if(p->next[id] == NULL)
{
p->next[id] = new trie();
}
p = p->next[id];
}
p->v = -1;
} int find_trie(char *str)
{
int i=0,j,id;
trie *p = root;
for(;*str != '\0';)
{
id= *str - 'a' ;
if (p->next[id] != NULL)
{
p = p->next[id];
if(p->v == -1 && *(str+1) == '\0')
return 1;
str++;
}
else
return 0;
}
return 0; } int find(char *str)
{
trie *p = root;
int m;
for (;*str != '\0';)
{
m = *str - 'a';
p = p->next[m];
if(p != NULL)
{
if (p->v == -1 && find_trie(str+1))
{
return 1;
}
str++;
}
else
return 0; }
return 0;
} int main()
{
int N,n,i=0,j,t,m,flag=0;
int a,b,c,d,k;
int sum;
trie *p;
while (gets(s[i]),s[i][0] != '\0')//,s[i][0] != '\0'
{
creat_trie(s[i++]);
}
for (j=0;j<i;j++)
{
if (find(s[j]))
{
puts(s[j]);
}
}
return 0;
}

代码2:map容器

#include <iostream>
#include <map>
#include <cstring>
using namespace std; map <string,int> m;
string s[50005]; int main()
{
int k=-1;
while(cin>>s[++k])
{
m[s[k]] = 1;
}
for(int i=0;i<=k;i++)
{
int len = s[i].length();
for(int j=1;j<len;j++)
{
string s1(s[i],0,j); //从0開始的j个数
string s2(s[i],j); //从j開始(不包含)一直到结尾
if(m[s1] == 1 && m[s2] == 1)
{
cout<<s[i]<<endl;
break;
} }
}
return 0;
}

Hat’s Words

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

Total Submission(s): 9436    Accepted Submission(s): 3369

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
 
Author
戴帽子的

HDU 1247 Hat’s Words (字典树 &amp;&amp; map)的更多相关文章

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  2. hdu 1247 Hat’s Words(字典树)

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

  3. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  4. Hdu 1247 Hat's Words(Trie树)

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

  5. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  6. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  7. hdu 1247:Hat’s Words(字典树,经典题)

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

  8. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  9. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. 【取对数】【哈希】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem J. Bobby Tables

    题意:给你一个大整数X的素因子分解形式,每个因子不超过m.问你能否找到两个数n,k,k<=n<=m,使得C(n,k)=X. 不妨取对数,把乘法转换成加法.枚举n,然后去找最大的k(< ...

  2. Linux提权exp大全

    如下表 #CVE #Description #Kernels CVE-2017-1000367 [Sudo] (Sudo 1.8.6p7 - 1.8.20) CVE-2017-7494 [Samba ...

  3. 【洛谷】NOIP提高组模拟赛Day1【组合数学】【贪心+背包】【网络流判断是否满流以及流量方案】

    U41568 Agent1 题目背景 2018年11月17日,中国香港将会迎来一场XM大战,是世界各地的ENLIGHTENED与RESISTANCE开战的地点,某地 的ENLIGHTENED总部也想派 ...

  4. alpha冲刺——代码规范、冲刺任务与计划(追光的人)

    代码规范 代码规范整合了自身项目实践还有诸多好的大公司的代码规范.如阿里巴巴开发手册.华为Java规范.W3C前端规范等. 由于内容过于详细和细致,为了方便查看,将其放置在了showDoc网站上(同时 ...

  5. WordPress 客户端软件列表

    Windows: BlogDesk BlogJet Blog Writer Chrysanth WebStory Deepest Sender (Firefox或SeaMonkey扩展,跨平台- De ...

  6. INTEL SSD SMART 性能 E8 E9 转

    如题今天研究了一下怎么看Intel SSD Toolbox里边显示的Smart信息.首先说,查看Intel SSD smart信息最好最方便的当然就是自家的工具箱,用其他工具查看不是不可以,但是很多数 ...

  7. delphi 如何判断应用程序未响应

    http://www.cnblogs.com/smallmuda/archive/2009/07/24/1529845.html delphi 如何判断应用程序未响应    今天在MSN的核心讨论组上 ...

  8. 安全:Web 安全学习笔记

    背景 说来惭愧,6 年的 web 编程生涯,一直没有真正系统的学习 web 安全知识(认证和授权除外),这个月看了一本<Web 安全设计之道>,书中的内容多是从微软官方文档翻译而来,这本书 ...

  9. H264 RTP封包原理(转载)

    1.  引言        随着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面临着另外一种不可避免的尴尬,就是在网络上看到生 ...

  10. [Android] repo 下载Android源码(国内镜像)

    reference : http://blog.csdn.net/shenlan18446744/article/details/51490560 repo 下载Android源码(国内镜像) 下载r ...