hdu1247-Hat’s Words-(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247
题意:给出一堆单词,求哪些单词是其中某两个单词拼接起来的。
题解:用字典树存储所有的单词,标记结束点,再次遍历单词的时候如果遍历过程遇到结束点则表明有单词是该单词的前缀,查找后半段字母(后缀)看看最后能不能遇到结束点,如果遇到了则该单词是某两个单词的前后缀,可以输出。详看注释。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<ctime>
#define ll long long
#define inf 0x3f3f3f3f
const double pi=3.1415926;
using namespace std;
int maxx=;
typedef struct node
{
int flag;///判断是否单词在这里就完了,作为前缀
node *next[];
};
node* root=new node();///根节点始终为空 void insert(char* s)
{
node* p=root;
int len=strlen(s);
for(int i=;i<len;i++)
{
int x=s[i]-'a';
if((p->next[x])==NULL)///当前遍历到的字母还没有开创节点
{
p->next[x]=new node();///新开一个节点
p=p->next[x];
}
else
{
p=p->next[x];
}
}
p->flag=;///标记有单词在这里结束
} int find2(char*s ,int now)///查后缀
{
node* p=root;
int len=strlen(s);
int i;
for(i=now;i<len;i++)///从now下标开始查询后缀,一直找
{
int x=s[i]-'a';
p=p->next[x];
if(p!=NULL)
{
if(i==(len-) && p->flag==)///s[now]-s[len-1]即后缀,是完整的单词
return ;
}
else
return ;
}
return ;
} int find(char* s)///查前缀,查到转后缀
{
node* p=root;
int len=strlen(s);
int i;
for(i=;i<len;i++)
{
int x=s[i]-'a';
p=p->next[x];///进入当前字母的节点
if(p!=NULL)
{
if(p->flag== && i!=(len-) && find2(s,i+))///有单词到这里断了,即有前缀,不能是这个单词本身,所以后续还要有字母
return ; ///如果find2成功才可以return 1,否则继续,可能还有别的前缀和后缀单词
}
else
return ;
}
return ;
} int main()///hdu1247 字典树
{
int i=;
char a[maxx][];
while(scanf("%s",a[i])!=EOF)
{
insert(a[i]);
i++;
}
for(int j=;j<i;j++)
{
if(find(a[j]))
printf("%s\n",a[j]);
}
return ;
}
没有看到纯数组模拟字典树的题解代码,不得不手动写指针,困扰了挺长时间。看到有用map和string解决的,感觉那不是正道,还是把字典树学了。
hdu1247-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---字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出 解题 ...
- HDU1247 - Hat’s Words(Trie树)
题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...
- HDu-1247 Hat’s Words,字典树裸模板!
Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...
- hdu1247(字典树+枚举)
Hat's Words(hdu1247) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 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非法访问. 代价 ...
随机推荐
- Docker入门之安装与使用
1. 安装(windows) win7.win8以及win10家庭版 等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirrors.aliy ...
- DirectShow 学习方法
DirectShow(简称 DShow) 是一个 Windows 平台上的流媒体框架,提供了高质量的多媒体流采集和回放功能. 这篇博客主要是简单讲下如何学习 Direct Show 框架,避免让自己少 ...
- TP框架where条件和whereOr条件同时使用
前言:where里面的条件是 && 的关系,whereOr里面的条件是 | | 的关系, 想要得到的效果: 1.筛选出is_deleted字段为0(未删除)的公告 2.筛选出全部状态为 ...
- docker容器的端口映射
1.创建一个Nginx 容器,先不映射端口 [root@localhost ~]# docker run --name my_nginx -d nginx 7be3673a4c0f8f7ffe79a7 ...
- Java 函数式编程—@FunctionalInterface----functional interface
单一函数接口,可以使用拉姆达表达式的形式具体化和实例化. 本质是将接口函数签名化. 如定义了一个函数式接口如下: @FunctionalInterface interface GreetingServ ...
- C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数
前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...
- C# NuGet常用命令
命令执行位置:工具=〉Nuget包管理器=〉程序包管理器控制台 一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安 ...
- 使用三层架构+EF添加单元测试
在运行测试的时候抛异常了: “System.InvalidOperationException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 The Entity Fram ...
- XMind破解版,2019年8月好使
越来越多的公司用思维导图了,进行编写测试用例,以下为破解版,亲身实验才发 的 ,中国时间2019年8月5日 下载安装包: 链接:https://pan.baidu.com/s/1-ubJLPSEpH ...
- 转 Fortofy扫描漏洞解决方案2
Fortify漏洞之Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Finally 继续对Fortify ...