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非法访问. 代价 ...
随机推荐
- CCF模拟题-201909
2.小明种苹果(续)(100分) #include<iostream> #include<cstdio> #include<cstring> #define max ...
- 常见算法合集[java源码+持续更新中...]
一.引子 本文搜集从各种资源上搜集高频面试算法,慢慢填充...每个算法都亲测可运行,原理有注释.Talk is cheap,show me the code! 走你~ 二.常见算法 2.1 判断单向链 ...
- [算法模版]子序列DP
[算法模版]子序列DP 如何求本质不同子序列个数? 朴素DP 复杂度为\(O(nq)\).其中\(q\)为字符集大小. \(dp[i]\)代表以第\(i\)个数结尾的本质不同子序列个数.注意,这里对于 ...
- Chrome保存整个网页为图片
打开需要保存为图片的网页 然后按F12,接着按Ctrl+Shift+P 在红框内输入full 点击下面的“Capture full size screenshot”就可以保存整个网页为图片了 原文出处 ...
- 数据竞争检查工具(TSan)
https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/ThreadSanitizerC ...
- CSS的基础学习
CSS学习 --------学习资源 http://www.csszengarden.com/ CSS语法检查http://jigsaw.w3.org/css-validator/ 配置CSS的方法: ...
- C# 中如何创建异步平行任务?
解释都在代码里,直接贴代码了: private async void btnStartRequestResource_Click(object sender, EventArgs e) { ShowA ...
- 使用 jQuery.TypeAhead 让文本框自动完成 (二)(访问远程数据)
项目地址:https://github.com/twitter/typeahead.js 直接贴代码了: @section headSection { <script type="te ...
- JVM的监控工具之jinfo
参考博客:https://www.jianshu.com/p/8d8aef212b25 jinfo(ConfigurationInfoforJava)的作用是实时地查看和调整虚拟机各项参数,使用jps ...
- 我是如何一步步编码完成万仓网ERP系统的(一)系统架构
https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...