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 ...
随机推荐
- 通过 ssh 登录到手机 Termux
通过ssh登录到手机 Termux 测试环境 电脑: macOS Mojave 手机: Huawei Mate10Pro Termux是Android上的一个非常强大的终端模拟器. 强大之处在于支持使 ...
- HTTP请求头和响应头
这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例. 1. HTTP请求头 accept:浏览器通过这个头告诉服务器,它所支持的数据类型.如:text/html, ima ...
- linux系统时间同步,硬件时钟和系统时间同步,时区的设置
1.时间同步(手动): date -s "2015-07-15 22:13:30" hwclock --systohc (表示系统时间同步到硬件时钟) hwclo ...
- 【Go入门教程7】面向对象(method、指针作为receiver、method继承、method重写)
前面两章我们介绍了函数和struct,那你是否想过函数当作struct的字段一样来处理呢?今天我们就讲解一下函数的另一种形态,带有接收者(receiver)的函数,我们称为method method ...
- 查看是否安装.NET Framework、.NET Framework的版本号、CLR版本号
查看是否安装.NET Framework→%SystemRoot%\System32→如果有mscoree.dll文件,表明.NET Framework已安装 查看安装了哪些版本的.NET Framw ...
- DTree的改进与使用经验
1.dtree.js源码 function Node(id, pid, name, url, title, target, icon, iconOpen, open) { this.id = id; ...
- Oracle常用系统查询SQL
以user1身份登录oracle,然后执行:select table_name from user_tables;或select table_name from tabs; 常用SQL --1.查询o ...
- google protocol buffer的原理和使用(二)
本文主要会介绍怎么使用Google Protocol的Lib来序列化我们的数据,方法非常多种,本文仅仅介绍当中的三种.其它的方法读者能够通过自行研究摸索.但总的来说,序列化数据总的来说分为下面俩步: ...
- OpenCV 脸部跟踪(2)
前面一篇文章中提到,我们在一副脸部图像上选取76个特征点,以及这些特征点的连通性信息来描述脸部形状特征,本文中我们会把这些特征点映射到一个标准形状模型. 通常,脸部形状特征点能 ...
- 第九章 JVM调优推荐
说明:本文主要参考自<分布式Java应用:基础与实践> 1.JVM的调优主要是内存的调优,主要调两个方面: 各个代的大小 垃圾收集器选择 2.各个代的大小 常用的调节参数 -Xmx -Xm ...