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非法访问. 代价 ...
随机推荐
- Paper | Dynamic Residual Dense Network for Image Denoising
目录 1. 故事 2. 动机 3. 做法 3.1 DRDB 3.2 训练方法 4. 实验 发表于2019 Sensors.这篇文章的思想可能来源于2018 ECCV的SkipNet[11]. 没开源, ...
- Python Dataframe 分组排序和 Modin
Python Dataframe 分组排序和 Modin 1.按照其中一列进行排序 在dataframe中,按照其中的一列排序:比如q值倒排 (1)rank方法 data['new_rank'] = ...
- torch_11_风格迁移和cycleGAN
1,A Neural Algorithm of atistic Style https://axiv.org/pdf/1508.06576.pdf 如何定义图片的内容,风格: 定义内容:在vggnet ...
- paramiko简介
一.什么是paramiko 要想明白什么是paramiko,要先明白ssh协议. 二.什么是ssh协议 ssh全称是Secure Shell (翻译:安全的外壳),根据字面意思就可以知道是和安全相关的 ...
- UVA 10790 How Many Points of Intersection? 组合数学
We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segment ...
- Knative 基本功能深入剖析:Knative Serving 自动扩缩容 Autoscaler
Knative Serving 默认情况下,提供了开箱即用的快速.基于请求的自动扩缩容功能 - Knative Pod Autoscaler(KPA).下面带你体验如何在 Knative 中玩转 Au ...
- 【02】Nginx:基本配置和日志处理
写在前面的话 Nginx 在安装完成后自动为我们生成了一个展示欢迎页的虚拟主机,除此之外,还附带了很多基础的配置,我们先来看看这些配置有什么用,顺便添加一些常用但是配置文件中并未初始化进去的配置来专门 ...
- SpringBoot多数据源动态切换数据源
1.配置多数据源 spring: datasource: master: password: erp_test@abc url: jdbc:mysql://127.0.0.1:3306/M201911 ...
- yii2.0的学习之旅(二)
前言:上一次我们简单认识了一下yii2.0安装,模型基本(增,删,改,查)操作 一.前后台数据交互 *如果你觉得默认的top样式太丑,可以这样关掉* *底部也可以这样关掉* (1)mvc合作操作数据 ...
- 2019-11-25-win10-uwp-通过命令行脚本开启旁加载
原文:2019-11-25-win10-uwp-通过命令行脚本开启旁加载 title author date CreateTime categories win10 uwp 通过命令行脚本开启旁加载 ...