字典树-HDOJ-1247-Hat’s Words

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8482    Accepted Submission(s): 3053

Problem Description

A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

You are to find all the hat’s words in a dictionary.

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.

Only one case.

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input

a
ahat
hat
hatword
hziee
word

Sample Output

ahat
hatword

大意:给定若干个字符串,找出这种字符串——它可由其它两个字符串拼接得到。

分析:考虑两种特殊情况:

input-----

a

bc

ab

c

abc

output----

abc

input-----

b

ba

baab

baabb

output----

baabb

时间: 2024-08-02 13:49:44

字典树-HDOJ-1247-Hat’s Words的相关文章

hdoj 1247 Hat’s Words(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的字符串. 对于长度为LEN的字符串,其可能由LEN种可能的拼接可能:现在问题转化为查找能够拼接成该字符串的可能的两个字符串是否都在 输入的字符串中,使用字典树可以实现快速的字符串查找,算法复杂度为O(N*M),N为输入字符串的个数,M为字符串长度. 代码如下: #include <cstdio>

字典树(trie 树)

字典树(trie树) 字典树是一种在字符串查找,前缀匹配等方面应用广泛的算法,它在查找字符串时只与被查询的字符串长度有关,所以它在查找时只有O(1)的时间复杂度,但随之而来的较大的空间复杂度. 一.原理分析 如图,字典树的每一个节点是由一个数据域(用来标记是否在此处有字符串终止)与26个指针域(表示26个小写字母)组成(PS:联想链表).每个节点表示一个字符,我们将我们将要输入的字符串插入字典树中,从根节点到某一节点(具有终止标记,上图红点),为已插入字符串,上图中的字符串有:abc.abcd.

HDU 1247 Hat&#39;s Words (字典树)

[题目链接]click here~~ [题目大意]A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. ,找出由两个子字符串组成的字符串. [解题思路]字典树 #include <bits/stdc++.h> using namespace std; const int N=5*1e4+100; const int MOD=

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

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

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. You are to find all the hat's words in a dictionary. Input Standa

HDU 1247 Hat&#39;s words(字典树Trie)

解题思路: 判断给出的单词是否恰好由另外两个单词组成,用栈保存每个子字符串的节点,从这个节点出发判断剩下的字符串是否在字典树中即可. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <sta

(字典树)HDU - 1247 Hat’s Words

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意:输出在所给字典中所有的hat's word.hat's word:是字典中由其他两个单词连接而成的的单词. 分析:现存下所有单词,然后遍历查询.用exist记录当前节点是否组成单词.当遍历某一个单词word时,如果在遍历结束前,在第i个查到当前前缀已经组成了单词,那么再从头遍历检查word.substr(i+1,word.length())[从第1个开到末尾组成的子串].因为可能存在多个

HDU 1247 Hat’s Words (字典树 &amp;amp;&amp;amp; map)

分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入的字符串组成的前后缀,自己根本没有推断前缀是否满足.就直接推断后缀,一直想不通自己哪里错了,非常羞愧,水平还是不行. 查找分为前缀查找和后缀查找.事实上两个函数基本差点儿相同的.以下放代码. #include <cstdio> #include <cstring> #include &

hdu 1247 Hat’s Words 字典树,还是比较有意思的题目

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8843    Accepted Submission(s): 3171 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl