题目链接: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. 数据库MySQL多个数据库服务冲突

    一.目标名称 MySQL 二.目标版本 mysql-5.6.24-win32.1432006610.zip 三.环境信息 系统:windows 7 旗舰版 防火墙:关闭  —— 注意:如果防火墙不关闭 ...

  2. linux杂记(十)what is BASH Shell

    first,what is shell?其实只要是碰过计算机的,对于OS(Operation System操作系统,不管是linux.unix.windows)有点概念的人大多都听过这个名词,因为只要 ...

  3. <正向/反向>最大匹配算法(Java)

    算法描述(正向): 给定最大词长n,待分词文本str,指针f=0,词典dic文档 1 取子串sub=str(f,f+n) 2 如果(遍历dic,有匹配sub) f++; 3 否则 n--; 4 注意: ...

  4. hdu 4292 Food 网络流

    题目链接 给你f种食物, 以及每种食物的个数, d种饮料, 以及个数, n个人, 以及每个人可以接受的食物种类和饮料种类. 每个人必须得到一种食物和一种饮料. 问最后得到满足的人的个数. 因为一个人只 ...

  5. JS笔记 入门第三

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树) 把上面的代码进行分 ...

  6. [C++]Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

  7. Html 小插件3

    搜狗搜索框代码 <script>function verifyquery(form){if(form.sogou_drop.value==2){form.insite.value='';} ...

  8. vb6.0 倒计时

    Dim t Dim start As Boolean Private Sub Command1_Click() If start = False Then t = Val(Text1) * 3600 ...

  9. ubuntu之iptables

    1.更新iptables并立即生效: a.保存当前设置:iptables-save > /etc/iptables.up.rules b.修改iptables规则: 例如: -I INPUT - ...

  10. 简单天气应用开发——解析HeWeather JSON

    借助和风天气API成功获取到了我想要的天气信息,但在怎么提取出我想要的数据上又遇到了问题. {"HeWeather data service 3.0":[{"basic& ...