hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247
思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的字符串。
对于长度为LEN的字符串,其可能由LEN种可能的拼接可能;现在问题转化为查找能够拼接成该字符串的可能的两个字符串是否都在
输入的字符串中,使用字典树可以实现快速的字符串查找,算法复杂度为O(N*M),N为输入字符串的个数,M为字符串长度。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int LEN = ;
const int MAX_N = + ;
const int KIND = ;
char str[MAX_N][LEN]; struct Node {
Node *next[KIND];
bool end;
Node()
{
memset(next, , sizeof(next));
end = false;
}
}; void Insert(Node *root, char *str)
{
int i = , k = ;
Node *p = root; while (str[i]) {
k = str[i] - 'a';
if (!p->next[k])
p->next[k] = new Node();
p = p->next[k];
++i;
}
p->end = true;
} bool Find(Node *root, char *str)
{
int i = , k = ;
Node *p = root; while (str[i]) {
k = str[i] - 'a';
p = p->next[k];
if (!p)
return false;
++i;
}
return p->end;
} int main()
{
int count = ;
Node *root = new Node();
char sub_str1[LEN], sub_str2[LEN]; while (scanf("%s", str[count]) != EOF)
Insert(root, str[count++]);
for (int i = ; i < count; ++i) {
int len = strlen(str[i]);
for (int j = ; j < len; ++j) {
strncpy(sub_str1, str[i], j);
sub_str1[j] = '\0';
strncpy(sub_str2, str[i] + j, len - j);
sub_str2[len - j] = '\0';
if (Find(root, sub_str1) && Find(root, sub_str2)) {
printf("%s\n", str[i]);
break;
}
}
}
return ;
}
hdoj 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 ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- 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 ...
- hdoj 1251 统计难题 【字典树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- 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(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
随机推荐
- C#中的ref与out参数(本文仅作为个人笔记)
假如一个方法的参数(形参)是引用类型,那么使用那个参数来进行的任何修改都会改变参数引用的数据.但这里的关键在于,虽然引用的数据发生了变化,但参数本生没有改变——它仍然引用的是一个对象.换句话说,虽然可 ...
- UITextField 光标的位置设置获取
UITextField 光标的位置设置获取 通过给UITextField 加一个拓展 //#import "UITextField+ExtentRange.h" #import & ...
- BZOJ 2324: [ZJOI2011]营救皮卡丘( floyd + 费用流 )
昨晚写的题...补发一下题解... 把1~N每个点拆成xi, yi 2个. 预处理i->j经过编号不超过max(i,j)的最短路(floyd) S->0(K, 0), S->xi(1 ...
- java——String的那边破事
经典的先看下面一段代码,请问最终创建几个对象,分别在哪里? String s0 = new String("luoliang.me"); String s1 = "luo ...
- ThinkPHP实现导出
刚开始做项目的时候,遇到了这个需求.说实话,对于一个才出来实习的菜鸟,而且还是才接触PHP的菜鸟而言,实在是有心杀敌,无力回天啊. 最简单的方法,就是网上找一个插件,然后一个本来就十几兆的项目,又增加 ...
- eclipse更改主题
长期使用eclipse,导致视觉疲劳,就想着能否换个主题调节调节. 通过设置window>preferences>appearance设置theme,貌似不起作用. 一查,发现一个绝佳的网 ...
- javascript 数组和字符串的转化
字符串转化为数组 'abcde' -> ['a', 'b', 'c', 'd', 'e'] 简单一点的方法,__String.prototype.split__可以将字符串转化为数组,分隔符为空 ...
- qcow2、raw、vmdk等镜像格式
转自 http://www.prajnagarden.com/?p=248 http://blog.csdn.net/starshine/article/details/8179483 转者言:对pr ...
- 浅析Thinkphp框架中运用phprpc扩展模式
浅析Thinkphp框架中应用phprpc扩展模式 这次的项目舍弃了原来使用Axis2做web服务端的 方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc ...
- sql语句开发使用---update
单表的更新大家都用过了,现在说下实际开发过程中,需要多表的查询更新状态,或者跨数据库的更新状态. 东西需要学习了才会懂得,所以站在巨人的肩膀看的更远. sql 语法; UPDATE 表名称 SET 列 ...