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

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

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

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

关于字典树的建立的话,因为他是不知道子树的,所以只要判断出来不存在子树就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. paramiko 模块安装和使用

    一.Centos安装Paramiko 1.安装组件 yum install openssl openssl-devel python-dev pycrypto -y yum install zlib- ...

  2. Git配置出现的问题

    git是代码版本同步工具,适用于团队开发,进公司第一堂课就是配置Git.接下来就把其中遇到的问题记录一下,与大家共享一下. 首先,在Bitbucket上注册账户,之后给管理员说一下,让他邀请你加入开发 ...

  3. kafka connect简介以及部署

    https://blog.csdn.net/u011687037/article/details/57411790 1.什么是kafka connect? 根据官方介绍,Kafka Connect是一 ...

  4. Windows读写文件的猫腻

    这里主要涉及对于回车换行的讨论. 回车:\r 换行:\n Windows读写文件分为普通文件读写和二进制文件读写. 如果以二进制的方式读写文件(如rb, wb),将会完全的把文件内容读出来,不做任何处 ...

  5. 022_Hadoop中的数据类型(Writable、WritableComparable、Comparator、RawComparator…)

    1. 在hadoop中所有的key/value都必须实现Writable接口,有两个方法,分别用于读(反序列化)和写(序列化)操作.

  6. five application :Labeling features

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Android开发BUG及解决方法2

    错误描述: 错误分析: 程序依赖的两个包冲突 解决方法: 在build.gradle文件中android节点下加packagingOptions节点

  8. LVS 命令使用

    LVS 命令使用 查询命令 ipvsadm -L # 查看lvs负载均衡信息ipvsadm -L -n # -n 查看IP端口ipvsadm -L -c   # 显示当前连接ipvsadm -L -- ...

  9. sql server parameter validation of stored procedure

    https://stackoverflow.com/questions/41908156/validating-missing-parameter-from-procedure-calls I don ...

  10. HBase-存储-文件

    文件 HBase使用一个HDFS中可配置的根目录,默认设为“/hbase”.可使用hadoop fs -lsr /hbase查看目录结构,文件可以被分为两类,一类位于HBase根目录下,另一类位于根目 ...