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 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
随机推荐
- oracle习题SQL语句练习
表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Varchar2(3) 否 学号(主码) Sname Varchar2(8) 否 学生姓名 Ssex Varchar2( ...
- ROS理解roslaunch命令
roslaunch命令从launch文件中启动一个节点,它的使用方法如下: roslaunch [package] [filename.launch] 首先切换到beginner_tutorials文 ...
- R与数据分析旧笔记(十八完结) 因子分析
因子分析 因子分析 降维的一种方法,是主成分分析的推广和发展 是用于分析隐藏在表面现象背后的因子作用的统计模型.试图用最少的个数的不可测的公共因子的线性函数与特殊因子之和来描述原来观测的每一分量 因子 ...
- 重拾javascript动态客户端网页脚本
笔记一: 一.DOM 作用: · DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...
- 59 pages的Delphi源码
http://www.codesc.net/source/list_10_59.shtml
- 调用Android自带日历功能(日历列表单、添加一个日历事件)
调用Android自带日历功能 觉得这篇文章不错,转载过来. 转载:http://blog.csdn.net/djy1992/article/details/9948393 Android手机配备有 ...
- 多名Uber司机被指刷单遭封号 一周薪水为0
昨天,一司机在Uber“司机之家”办公地墙上写了泄愤的话 摄/法制晚报记者 苏妮 司机展示的账单显示,上周的薪水几乎为零,上面用英文标注了“欺诈行为”的字样 摄/法制晚报记者 苏妮 法制晚报讯(记者 ...
- Codeforces Round #242 (Div. 2) <A-D>
CF424 A. Squats 题目意思: 有n(n为偶数)个x和X,求最少的变换次数,使得X的个数为n/2,输出变换后的序列. 解题思路: 统计X的个数ans,和n/2比較,少了的话,须要把n/2- ...
- Swift和Objective-C混合编程
假设你现在就是一个iOS程序员,你对Objective-C很熟悉,对iOS开发也很熟悉,然而,苹果公司在iOS 8之后推出了Swift语言.那么,如何才能快速地从Objective-C过渡到Swift ...
- LeetCode Coins in a Line
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...