HDU 1247 Hat’s Words(字典树变形)
题目链接: pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247
You are to find all the hat’s words in a dictionary.
Only one case.
a
ahat
hat
hatword
hziee
word
ahat
hatword
题意:
寻找出单次表中一个单词是由另外两个单词组成的。
代码例如以下:
#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 26
char str[50017][117];
typedef struct Trie
{
int v;//依据须要变化
Trie *next[MAXN];
//next是表示每层有多少种类的数,假设仅仅是小写字母,则26就可以,
//若改为大写和小写字母,则是52,若再加上数字,则是62了
}Trie;
Trie *root; void createTrie(char *str)
{
int len = strlen(str);
Trie *p = root, *q;
for(int i = 0; i < len; i++)
{
int id = str[i]-'a';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
q->v = 1;//初始v==1
for(int j = 0; j < MAXN; j++)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
// p->next[id]->v++;
p = p->next[id];
}
}
p->v = -1;//若为结尾,则将v改成-1表示
} int findTrie(char *str)
{
int len = strlen(str);
Trie *p = root;
for(int i = 0; i < len; i++)
{
int id = str[i]-'a';
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return 0;
// if(p->v == -1) //字符集中已有串是此串的前缀
// return -1;
}
//return -1; //此串是字符集中某串的前缀
if(p->v == -1)//说明是全然匹配
return -1;
else
return 0;
}
int dealTrie(Trie* T)
{
//动态字典树,有时会超内存,这是就要记得释放空间了
if(T==NULL)
return 0;
for(int i = 0; i < MAXN; i++)
{
if(T->next[i]!=NULL)
dealTrie(T->next[i]);
}
free(T);
return 0;
}
int main()
{
root = (Trie *)malloc(sizeof(Trie));
for(int i = 0; i < MAXN; i++)
root->next[i] = NULL;
int cont = 0;
while(scanf("%s",str[cont])!=EOF)
{
createTrie(str[cont]);
cont++;
}
char a[117], b[117];
for(int i = 0; i < cont; i++)
{
int len = strlen(str[i]);
for(int j = 1; j < len; j++)
{
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
strncpy(a,str[i],j);
strncpy(b,str[i]+j,len-j);
if(findTrie(a)==-1 && findTrie(b)==-1)
{
printf("%s\n",str[i]);
break;
}
}
}
return 0;
}
HDU 1247 Hat’s Words(字典树变形)的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- Hdu 1247 Hat's Words(Trie树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
- HDU 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)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
随机推荐
- 使用router.push()进行页面跳转的问题
看着官网的文档直接router.push()这样会报错router undefind,需要写成this.$router.push()才可以
- bzoj 2788 [Poi2012]Festival 差分约束+tarjan+floyd
题目大意 有n个正整数X1,X2,...,Xn,再给出m1+m2个限制条件,限制分为两类: 1.给出a,b (1<=a,b<=n),要求满足Xa + 1 = Xb 2.给出c,d (1&l ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- La 3942 字符串+dp
题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法? #include<iostream> #include<cstdio> #includ ...
- GridControl 控制列中的控件显示
一.需求描述 根据条件判断Checkbox的显示,先上个图,, 类似Demo中的这个,因为不能控制文本的显示,所以需对该列的其它事件做些处理, 二.解决方案 1.添加GridControl上需要的控 ...
- CodeForces 97D. Robot in Basement
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- VMware锁定文件失败开启模块diskearly的操作失败未能启动虚拟机
删除虚拟机目录下的(如图中标明的就是D:\VMWorks\YeZiZxWeb这个目录)三个 *.lck文件夹,启动正常
- 标准C程序设计七---120
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Scrapy学习-11-Selector对象使用
Selector使用 使用背景 我需要使用类似spider项目中,response使用的xpath和css获取页面指定数据,但因为爬取页面较小我们不想创建一个spider项目时,就可以使用scrapy ...
- 转载:linux编程,命令行参数输入getopt
下面资料来自百度百科: getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const ...