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 ...
随机推荐
- HDU 4730 We Love MOE Girls (2013成都网络赛,签到水题)
We Love MOE Girls Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Namespace declaration statement has to be the very first statement in the script
php 中 Namespace declaration statement has to be the very first statement in the script 错误解决方法: 在PHP文 ...
- java基础知识概要图
- nginx简单代理配置
原文:https://my.oschina.net/wangnian/blog/791294 前言 Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器, ...
- DIOCP
DIOCP GITHUB: https://github.com/ymofen/diocp-v5.git diocp5====== ## 快速开始 从那里得到: git更新(推荐同步更新) 1.htt ...
- 【转载】如果快速开发APP&创业
先贴原文所在个人博客: http://uikoo9.com/ 今天看了一些这个人的文章,还是有一定见解的,比如下面这篇 <如何快速开发出一个高质量的APP——创业谈> http://uik ...
- spring boot常用注解使用小结
1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Sprin ...
- python 读写CSV文件
#-*- coding: UTF-8 -*- import csv import os def WriteToCsv(): '''写CSV文件''' titls = ['序号', '链接', '备注' ...
- Cognos定时Email发送报表数据功能
1:进入 IBM Cognos Configuration-Data Access-Notification 2:设置如下(注意一定要是smtp服务,端口25,我这里是用了腾讯邮箱的smtp服务) 当 ...
- 在Hadoop上运行基于RMM中文分词算法的MapReduce程序
原文:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-count-on-hadoop/ 在Hadoop上运行基于RMM中文分词 ...