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

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

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

#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. Web2.0应用程序的7条原则

    个人看好Web的发展潜力,本文字摘自<Collective Intelligence 实战> 网络是平台 使用传统许可模式软件的公司或用户必须运行软件.定期更新至最新版本,以及扩展它来满足 ...

  2. android 单位 什么是屏幕密度?

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha sp dp px in in 表示英寸, 是屏幕的物理尺寸.1英寸是2.54厘米. dp ...

  3. Express应用程序目录结构

    1.Node安装与使用 网上有很多Node的安装教程,可以做参考 2.MongoDB的安装与使用 MongoDB安装也有很多教程,可以去网上找找 3.初始化一个express项目 使用express框 ...

  4. Apache之.htaccess备忘录(二)

    博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我. 转载请注明"深蓝的镰刀" 书接上回,<Apache之.htaccess备忘录(一)& ...

  5. CentOS7.0安装Nginx-1.12.0

    一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...

  6. LoadRunner的简单使用《第一篇》

    LoadRunner是一个用压力测试的软件.这东西比较难上手,光安装就非常麻烦.好不容易一步步跟着安装说明安装好之后,还是用不了. 记录一个问题如下: 导入脚本的时候报错fail to create ...

  7. 垃圾收集简介 - GC参考手册

    http://blog.csdn.net/column/details/14851.html

  8. 在EntityFramework6中管理DbContext的正确方式——4DbContextScope:一个简单的,正确的并且灵活的管理DbContext实例的方式(外文翻译)

    (译者注:使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gu ...

  9. 用资源管理器右键编译 Visual Studio 解决方案文件

    每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...

  10. datagrid在MVC中的运用03-选择单行或多行

    本文体验datagrid显示单行或多行内容.分别用到了datagrid的getSelected,getSelections方法. Html部分 <a href="#" cla ...