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): 8579    Accepted Submission(s): 3090

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

Author

戴帽子的

题意:给定一些单词(按字典序给出), 按字典序输出所有满足条件的单词(条件为该单词由其它两个单词构成)

思路:先构造一颗字典树,,然后再依次判断该单词是否有其他两个单词组成,,判断方法为在该单词中找其中存在p->is为true的点,然后将点+1放入栈中,看是否这个点往后能组成一个其他单词,是则输出,,不是则不输出。。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

const int MAX = 50005;
char word[MAX][30];

struct node
{
	bool is;
	struct node *next[26];
	node()
	{
		is = false;
		memset(next, 0, sizeof(next));
	}
};

int insert(node *root, char *s)
{
	int i = 0;
	node *p = root;
	while(s[i])
	{
		if(p->next[s[i]-'a'] == NULL)
			p->next[s[i]-'a'] = new node;
		p = p->next[s[i]-'a'];
		i++;
	}
	p->is = true;
}

int search(node *root, char* s)
{
	node *p = root;
	int i = 0;
	stack<int> t;
	while(s[i])
	{
		if(p->next[s[i]-'a'] == NULL) return 0;
		p = p->next[s[i]-'a'];
		if(p->is && s[i+1])
			t.push(i+1);
		i++;
	}
	while(!t.empty())
	{
		bool ok = 1;
		i = t.top(); t.pop();
		p = root;
		while(s[i])
		{
			if(p->next[s[i]-'a'] == NULL)
			{
				ok = 0;
				break;
			}
			p = p->next[s[i]-'a'];
			i++;
		}
		if(ok && p->is) return 1;
	}
	return 0;
}

int main()
{
	int num = 0;
	node* root = new node();
	while(scanf("%s", word[num]) != EOF)
	{
		insert(root, word[num]);
		num++;
	}
	for(int i=0; i<num; i++)
		if(search(root, word[i]))
			printf("%s\n", word[i]);
	return 0;
} 
时间: 2024-10-03 02:42:22

HDU - 1247 - Hat’s Words (字典树!!)的相关文章

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): 8843    Accepted Submission(s): 3171 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

HDU 1247 Hat’s Words (字典树&#183;Trie)

题意  给你一个字典  输出字典中能表示成两个单词连接的所有单词 最基础的字典树应用  先把所有单词加入字典树中  标记每个结点是否为某个单词的结尾  然后查找每个单词  在树上查询过程中遇到单词结尾时  如果剩下的后缀也是一个单词  那当前查询的单词就可以是两个单词的连接了 #include <cstdio> #include <cstring> using namespace std; const int N = 50005; int n; char ss[N][20]; st

hdu 1247 Hat’s Words (字典树模板)

//那个单词是有出现的两个单词构成的 # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50

hdoj 1247 Hat’s Words(字典树)

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

hdu 1247 Hat’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a

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(字典树,经典题)

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&#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(map,STL,字符处理,string运用)

题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<math.h> #include <iostream> #include<map> using namespace std; string word[50010]; int main() { i