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 ...
随机推荐
- DataTable添加行的方法
方法一: DataTable tblDatas = new DataTable("Datas");DataColumn dc = null;dc = tblDatas.Colum ...
- CentOS 5.8 上安装 systemtap-2.6 转
http://segmentfault.com/a/1190000002541077#articleHeader1
- Linux下open与fopen的区别
int open(const char *path, int access,int mode) path 要打开的文件路径和名称 access 访问模式,宏定义和含义如下: ...
- 第三方网站返回hybrid app H5页面缓存问题应对策略
最近负责公司各产品线购买模块的开发,各项功能如期开发完成后测试那边反馈回来一个问题:IOS手机在点击支付宝购买后,跳转到支付宝网站时不输入支付密码,直接点返回,返回到我们自己的APP购买界面发现页面显 ...
- redux 个人整理
序 本人工作之余的闲暇时间还是很充裕的.在完成经理安排的任务后,基本上都是在自学,主要是阅读各种技术文档.浏览博客.运行别人写的一些前端demo并观赏与赞叹.在ScorpionJay 同学的带领下,我 ...
- linux内核数据包转发流程(一):网络设备驱动
[版权声明:转载请保留出处:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 网卡驱动为每一个新的接口在一个全局的网络设备列表里插入一个数据结构.每一个接口由一 ...
- java.lang.UnsatisfiedLinkError:no dll in java.library.path终极解决之道
Java调用Dll时,会出现no dll in java.library.path异常,在Java Project中不常见,因为只要将Dll拷贝到system32目录下即可: 但若是 ...
- table固定首行(二)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第一章 EL表达式常见用法
el最常用的几种使用场景: 从配置文件中读取属性 缺失值情况下,配置默认值 el内部字符串使用String的方法 三目运算符 正则表达式 注入系统属性(system properties) 调用系统原 ...
- Length of Last Word leetocde java
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...