Hat’s Words(字典树的运用)
个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天,
不得不佩服这些发明算法人的大脑。
这题的解决方法还是从网上找到的,还好算法是自己实现得,没错!组合体只要进行分割再暴力搜索就好了,
步骤就是根据得到的字符串建立字典树,然后一一找寻时,一次拆开,只要俩边都满足在字典树里面找的到就是我们所要找的了。
关于字典树的建立的话,因为他是不知道子树的,所以只要判断出来不存在子树就malloc一个就好了,
注意指针的指向和递归的奥妙,很多更新都是在递归中完成的,需要好好的体会。
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(字典树的运用)的更多相关文章
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- hdu1 247 Hat’s Words(字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- JavaScript 的简单学习2
AJAX 一 AJAX预备知识:json进阶 1.1 什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javas ...
- C# TreeView,递归循环数据加载到treeView1中
TblAreaBLL bll = new TblAreaBLL(); private void button1_Click(object sender, EventArgs e) { LoadData ...
- nginx负载均衡详情
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- Android摄像头测量尺(Advanced Ruler Pro)使用方法
http://www.cnblogs.com/sinojelly/archive/2010/08/13/1799341.html Advanced Ruler Pro是一个Android手机应用程序, ...
- HAproxy 源码包安装
HAproxy 源码包安装 系统环境:Centos 7 x64位 服务版本:haproxy-1.7.8.tar.gz 编译工具:gcc 下载地址 HAproxy:https://pan.baidu.c ...
- CSS3动画表单
在线演示 本地下载
- complexHeatmap包画分类热图
用途:一般我们画热图是以连续变量作为填充因子,complexHeatmap的oncopoint函数可以以类别变量作为填充因子作热图. 用法:oncoPrint(mat, get_type = func ...
- R的基础学习之数据结构
来源:http://blog.qiubio.com:8080/archives/3753/4 1.atomic vector :一维的,放置同一类型数据的数据类型 1.1创建:由c()函数 ,seq( ...
- 外网IP地址API
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- 解决Newtonsoft.Json版本问题
在配置文件中添加以下代码,App.config或Web.config <runtime> <assemblyBinding xmlns="urn:schemas-micro ...