Hat’s Words(字典树)
You are to find all the hat’s words in a dictionary.
Only one case.
a
ahat
hat
hatword
hziee
word
ahat
hatword
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct Trie {
int flag;
Trie* next[26];
} Trie;
Trie* root;
int flag;
char a[50000][50];
void Inti()//初始化
{
root = (Trie*)malloc(sizeof(Trie));
root->flag = 0;
for (int i = 0; i < 26; i++)
root->next[i] = NULL;
}
void ChaRu(char *a)//建立字典树 插入单词
{
Trie* p = root, *q;
int l = strlen(a);
for (int i = 0; i < l; i++) {
int id = a[i] - 'a';
if (p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(Trie));
q ->flag = 0;
for (int i = 0; i < 26; i++)
q->next[i] = NULL;
p->next[id] = q;
}
/* else if (p->next[id]->flag) {
flag = 1;
}*/
p = p->next[id];
}
p->flag = 1;
}
int CZ(char *a)//查找单词
{
Trie *p=root;
int len=strlen(a);
for(int i=0;i<len;i++)
{
int id=a[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
}
return p->flag;
}
int SF(Trie *p)//释放空间 这个题不用也能够过
{
// Trie *p=root;
if(p==NULL)return 0;
for(int i=0;i<26;i++)
{
if(p->next[i]!=NULL)
SF(p->next[i]);
}
free(p);
return 0;
}
int main()
{ Inti();
int n=0;
while (~scanf("%s", &a[n])) {
// flag = 0;
//printf("%s",a[i]);
ChaRu(a[n]);
n++;
} for(int i=0;i<n;i++)
{
int len=strlen(a[i]);
for(int j=0;j<len;j++)
{
char str1[50]={'\0'},str2[50]={'\0'};//新建两个暂时数组
strncpy(str1,a[i],j); //把第i个单词进行拆分,分别放入两个暂时数组里面
strncpy(str2,a[i]+j,len-j);
if(CZ(str1)&&CZ(str2)) //通过查找函数进行查找。假设两个返回都是1,代表能够组成
{
printf("%s\n",a[i]);
break;
}
}
}
SF(root);//释放内存(这个题能够不要)
return 0;
}
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 ...
- 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 ...
- Hat’s Words(字典树的运用)
个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- MAX II Device Compatibility with 5.0-V CMOS Devices
http://www.altera.com/literature/hb/max2/max2_mii51009.pdf The open-drain pin never drives high, onl ...
- .net 基于Jenkins的自动构建系统开发
先让我给描述一下怎么叫一个自动构建或者说是持续集成 : 就拿一个B/S系统的合作开发来说,在用SVN版本控制的情况下,每个人完成自己代码的编写,阶段性提交代码,然后测试-修改,最后到所有代码完工,进行 ...
- max_binlog_cache_size
http://blog.mimvp.com/2017/07/mysql-yi-ge-can-shu-yin-qi-de-dang-ji-xue-an/ max_binlog_cache_size 表示 ...
- rac 10g 10.2.0.1升级到10.2.0.5具体解释
RAC 10.2.0.1 升级到 10.2.0.5 一. 准备: Patch 包:p8202632_10205_LINUX.zip 节点数:3个节点 RAC1 RAC2 ...
- 解决小米手机不能运行Android Studio程序的问题
转载自:解决小米手机不能运行Android Studio程序的问题 问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: ...
- 安装程序不能验证Update.inf文件的完整性,请确定加密服务正在此计算机上执行
近期安装Microsoft .NET Framework 4(独立安装程序)时,提示"安装程序不能验证Update.inf文件的完整性,请确定加密服务正在此计算机上执行" 没法放狗 ...
- EF Code First 学习笔记:表映射 多个Entity到一张表和一个Entity到多张表
多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...
- pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)
前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...
- java实时监控mysql数据库变化
对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...
- [翻译] SWTableViewCell
SWTableViewCell An easy-to-use UITableViewCell subclass that implements a swippable content view whi ...