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非法访问. 代价 ...
随机推荐
- 洛谷P4169 [Violet]天使玩偶/SJY摆棋子
%%%神仙\(SJY\) 题目大意: 一个二维平面,有两种操作: \(1.\)增加一个点\((x,y)\) \(2.\)询问距离\((x,y)\)曼哈顿最近的一个点有多远 \(n,m\le 300 0 ...
- Physically Based Shader Development for Unity 2017 Develop Custom Lighting Systems (Claudia Doppioslash 著)
http://www.doppioslash.com/ https://github.com/Apress/physically-based-shader-dev-for-unity-2017 Par ...
- 封装cookie的设置和获取
cookie的设置 function setCookie(key,value,options){ options=options||{}; var time=""; if(opti ...
- Xshell删除键不好使:删除显示退格^H
Xshell删除键不好使:删除显示退格^H 1.问题: Xshell不能删除,删除时出现 退格^H 2.解决方案: 点击上方:文件→属性→终端→键盘,把 delete 和 backspace 序列改为 ...
- 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)
1. 二叉树最近公共祖先 奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...
- linux jdk1.8 64位下载永久地址,ubuntu,centos,java
https://pan.baidu.com/s/1A4cl3vUWCtiHxJ9eHK2ApQ 密码:j8dg
- COMP 2406 – F19
COMP 2406 – F19 – A4 Due Friday, November 22nd at 11:59 PMAssignment 4 Trivia Quiz BuilderSubmit a s ...
- 【大数据】SparkSql 连接查询中的谓词下推处理 (一)
本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/YPN85WBNcnhk8xKjTPTa2g 作者:李勇 目录: 1.SparkSql 2.连接查询和 ...
- Anaconda--在虚拟环境中安装CUDA and cudnn
在conda虚拟环境中安装CUDAconda install cudatoolkit=8.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs ...
- 奥展项目笔记07--vue绑定下拉框和checkbox总结
1.vue绑定下拉框 <div class="col-md-1 data"> <select class="form-control " v- ...