题目链接: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. 对于System.Net.Http的学习(一)——System.Net.Http 简介(转)

    最新在学习System.Net.Http的知识,看到有篇文章写的十分详细,就想转过来,自己记录下.原地址是http://www.cnblogs.com/chillsrc/p/3439215.html? ...

  2. C++中虚函数功能的实现机制

    要理解C++中虚函数是如何工作的,需要回答四个问题. 1.  什么是虚函数. 虚函数由于必须是在类中声明的函数,因此又称为虚方法.所有以virtual修饰符开始的成员函数都成为虚方法.此时注意是vir ...

  3. Responsive Design响应式网站设计心得笔记

    这个词已经喊了很久了,一直都是小打小闹,没正经的做过大的响应式全站,这次终于有机会了.网站刚上线半个月,就要改版为响应式设计,支持手机/PC等各类终端显示浏览.今天把首页做好,并测试无误,这里把一些应 ...

  4. Bash shell使用环境的终端的环境设置:stty

    Bash shell使用环境的终端的环境设置:stty Bash shell使用环境的终端的环境设置:stty stty -a 将当前所有的stty参数列出来 intr:给正在运行的程序发送中断信号 ...

  5. MarkDown使用 (三)表格

    MarkDown表格的用法 MarkDown表格的用法 例如: $$ \begin{array}{c|lcr} n & \text{Left} & \text{Center} & ...

  6. 让IE6也能智能控制图片最大宽、高度

    当一个图片的宽度或高度超出了容器时,我们一般会用max-width或max-height来设置其最大宽.高度,让图片不会超出容器,但是如果同时设置了最大高度和最大宽度时,有可能会造成图片最终显示会有些 ...

  7. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  8. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)

    In Build Settings are: Architectures: Starndard (armv7, armv7s) Base SDK: Latest iOS (iOS 6.0) Build ...

  9. SQL Server 多表删除

    第一步: 建表 create table t1(x int, y int); create table t2(x int, y int); go insert into t1(x,y) values( ...

  10. Linux ---> 监控JVM工具

    Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...