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)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
随机推荐
- Java 微信公众号开发--- 接入微信
开发微信公众号在没有正式的公众平台账号时,我们可以使用测试平台账号--- 测试平台申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...
- grep用法详解:grep与正则表达式【转】
转自:http://blog.csdn.net/hellochenlian/article/details/34088179 grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配 ...
- os.system() 和 os.popen()
1.os.popen(command[, mode[, bufsize]]) os.system(command) 2.os.popen() 功能强于os.system() , os.popen() ...
- Spring MVC学习一
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请 ...
- poj 2253(kruskal)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34968 Accepted: 11235 Descrip ...
- LeetCode OJ--Best Time to Buy and Sell Stock II
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 第二问,是说可以进行无数次买卖. 贪心法 #include &l ...
- pdf转word工具
pdf转word工具及安装:http://blog.sina.com.cn/s/blog_6172011c0102vxir.html pdf去加密:http://www.downxia.com/dow ...
- 实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎
实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎 Programmatic Framework™ 传漾程序化购买框架
- DELPHI跨平台编译开关
DELPHI跨平台编译开关 DELPHI 现在是跨平台的开发工具,已经不仅仅针对WINDOWS OS. 跨平台的时候,一些WINDOWS特有的API或语法是不能用的,必须使用跨平台的新语法,要用编译开 ...
- 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)
http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...