题目链接: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(字典树)的更多相关文章

  1. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  2. hdu 1247 Hat’s Words(字典树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. Hat’s Words(字典树)

    Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...

  4. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  5. 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 ...

  6. hdoj 1251 统计难题 【字典树】

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  8. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

随机推荐

  1. IOS开发之Cocoa编程—— NSUndoManager

    在Cocoa中使用NSUndoManager可以很方便的完成撤销操作.NSUndoManager会记录下修改.撤销操作的消息.这个机制使用两个NSInvocation对象栈. NSInvocation ...

  2. GCD的使用和面试题集锦

    GCD 分为异步和同步 异步: dispatch_async ( 参数1 , { } 同步: dispatch_sync( 参数1 , { } 参数1 :队列 队列分为两种: dispatch_get ...

  3. errorPlacement的位置问题

    做一个前端的验证,使用了JQUERY.Validate 在errorPlacement上纠结了半天: 百度大多数都是一个答案: errorPlacement: function(error, elem ...

  4. hadoop hdfs 一些命令记录

    1.列出目录下的对象:hadoop fs -ls /lib 2.统计文件行数:hadoop fs -cat  /文件* | wc -l 3.统计文件或者目录大小:hadoop fs -count /l ...

  5. mysqli 启动出错

    innodb_buffer_pool_size = 512M  配置问题 /usr/local/mysql/bin/mysqld_safe --relay-log nor --relay-log-in ...

  6. Vim 实用技术,第 2 部分: 常用插件(转)

    http://blog.jobbole.com/20619/ 2.1. gzip(压缩文件支持) 作者:Bram Moolenar 网站脚本编号:无(包含在 Vim 的标准发布之中) 安装说明:无 功 ...

  7. Using SetWindowRgn

    Using SetWindowRgn Home Back To Tips Page Introduction There are lots of interesting reasons for cre ...

  8. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  9. xcode Simulated Metrics xib设置小问题

  10. VS2015自定义注释内容

    一直想自动添加一些注释信息,找了好多种方式:各种插件什么的,最后偶然发现可以修改vs的模板可以做到,下面介绍如何改 首先找到vs的安装目录,如下是我的安装目录: D:\Program Files\VS ...