个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天,

不得不佩服这些发明算法人的大脑。

这题的解决方法还是从网上找到的,还好算法是自己实现得,没错!组合体只要进行分割再暴力搜索就好了,

步骤就是根据得到的字符串建立字典树,然后一一找寻时,一次拆开,只要俩边都满足在字典树里面找的到就是我们所要找的了。

关于字典树的建立的话,因为他是不知道子树的,所以只要判断出来不存在子树就malloc一个就好了,

注意指针的指向和递归的奥妙,很多更新都是在递归中完成的,需要好好的体会。

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.

OutputYour 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
 #include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>
#include <queue>
#include<string>
using namespace std;
const int length=;
const int maxn=;
struct word
{
word *next[length];
bool book; };
void insertword(word *root,char *s)
{
if(root==NULL||*s=='\0')
return ;
word *p=root;
while(*s!='\0'){
if(p->next[*s-'a']==NULL){
word *q=(word *)malloc(sizeof(word));
for(int i=;i<length;i++)
q->next[i]=NULL;
q->book=false;
p->next[*s-'a']=q;
p=p->next[*s-'a'];
}
else
{
p=p->next[*s-'a']; }
s++;
}
p->book=true; }
bool check(word *root,char x[],int y,int z){
word *p=root;
for(int i=y;i<=z;i++)
{
if(p->next[x[i]-'a']==NULL) return false;
p=p->next[x[i]-'a'];
}
if(p->book==true) return true;
else return false; }
bool searchword(word *root, char *s)
{
char x[];
int i=;
while(*s!='\0')
{
x[i++]=*s;
s++;
}
for(int j=;j<i-;j++)
{
if(check(root,x,,j)&&check(root,x,j+,i-)){
return true;
}
}
return false; }
char st[][];
int main()
{
word *root=(word *)malloc(sizeof(word));
root->book=false;
int i;
for(i=;i<length;i++)
root->next[i]=NULL;
i=;
while(scanf("%s",st[i])!=EOF)
{
insertword(root,st[i]);
i++;
}
for(int j=;j<i;j++)
{
if(searchword(root,st[j]))
cout<<st[j]<<endl;
}
return ;
}
												

Hat’s Words(字典树的运用)的更多相关文章

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

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

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

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

  3. Hat’s Words(字典树)

    Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...

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

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

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

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

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

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

  7. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...

  8. hdu1 247 Hat’s Words(字典树)

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

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

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

随机推荐

  1. PAT 天梯赛 L1-046. 整除光棍 【模拟除法】

    题目链接 https://www.patest.cn/contests/gplt/L1-046 思路 用同余定理以及模拟除法. AC代码 #include <iostream> #incl ...

  2. PAT 天梯赛 L1-037. A除以B 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...

  3. json教程系列(2)-生成JSONObject的方法

    生成JSONObject一般有两种方式,通过javabean或者map类型来生成.如下面的例子:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...

  4. Linux Shell基础 环境变量配置文件

    source命令:使环境变量配置文件强制生效 source 命令会强制执行脚本中的全部命令,而忽略脚本文件的权限.该命令主要用于让重新配置的环境变量配置文件强制生效.source 命令格式如下: [r ...

  5. TIJ读书笔记03-初始化和构造器

      TIJ读书笔记03-初始化和构造器 初始化和清理是涉及安全的两个问题,如果对象不能正确的初始化会引起很多错误,比如空指针异常等,如果不能恰当及时的清理,会占用过多资源. 构造器在创建一个类的实例的 ...

  6. Jfreechart 生成不同数据源多个饼图(Multiple Pie Chart)

    http://blog.163.com/ppy2790@126/blog/static/103242241201210130736274/ 项目中要用JfreeChart实现不同数据源多个饼图展现每个 ...

  7. Centos系统 上下文切换的检查思路

    1.什么是上下文切换(Context Switch)? 上下文切换,有时也称做进程切换或任务切换,是指CPU从一个进程或线程切换到另一个进程或线程. 操作系统可以同时运行多个进程, 然而一颗CPU同时 ...

  8. linux之下载工具wget

    常用格式:wget options URL -b,  --background        启动后转入后台执行 -c,  --continue               接着下载没下载完的文件 - ...

  9. CentOS 6.5 下的截图方法

    1.利用命令模式 捕获整个屏幕 : $ gnome-screenshot 截完屏之后我们可以设置自定义图片存储位置,如图: 捕获当前终端Terminal : $ gnome-screenshot -w ...

  10. 基于Visual c++ 2012的php扩展开发 - 环境搭建

    软件准备 Apache2.4 php-5.6.20-Win32-VC11-x86 php-5.6.20-src mysql-5.5.45-win32 vcredist_x86.exe vs2012旗舰 ...