HDU 1247 Hat's words(Trie)

HDU 1247 Hat‘s words(Trie)

ACM

题目地址:

HDU 1247 Hat‘s words

题意:

给些单词,问每个单词是否能用另外两个单词拼出。

分析:

直接保存到trie里面,然后暴力切割查询即可。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  File:        1247.cpp
*  Create Date: 2014-09-24 11:04:11
*  Descripton:
*/

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

#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;

const int N = 50010;
const int MAXNODE = 1000010;
const int MAXSON = 26;

// array index
struct ATrie {
	int ch[MAXNODE][MAXSON];
	int val[MAXNODE];
	int sz;		// num of nodes
	ATrie() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); }
	void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); }
	inline int idx(char c) { return c ? c - 'a' : 0; }

	void insert(char *s, int v = 1) {
		int u = 0, len = strlen(s);
		repf (i, 0, len - 1) {
			int c = idx(s[i]);
			if (!ch[u][c]) {
				memset(ch[sz], 0, sizeof(ch[sz]));
				val[sz] = 0;
				ch[u][c] = sz++;
			}
			u = ch[u][c];
		}
		val[u] = v;
	}

	// if s in trie return the value, else return 0
	int find(char *s) {
		int u = 0, len = strlen(s);
		repf (i, 0, len - 1) {
			int c = idx(s[i]);
			if (ch[u][c])
				u = ch[u][c];
			else
				return 0;
		}
		return val[u];
	}
} trie;

char wd[N][110], a[110], b[110];
int n;

int main() {
	// ios_base::sync_with_stdio(0);
	while (gets(wd[n])) {
		trie.insert(wd[n++]);
	}
	repf (i, 0, n - 1) {
		int len = strlen(wd[i]);
		repf (j, 1, len - 1) {
			strncpy(a, wd[i], j);
			a[j] = 0;
			strcpy(b, wd[i] + j);
			// cout << a << ' ' << b << endl;
			if (trie.find(a) && trie.find(b)) {
				printf("%s\n", wd[i]);
				break;
			}
		}
	}
	return 0;
}

HDU 1247 Hat's words(Trie)

时间: 2024-10-12 03:20:42

HDU 1247 Hat's words(Trie)的相关文章

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(从给的单词中找hat&#39;s word 并按字典序输出)

1.在使用mp[key]的时候它会去找键值为key的项,如果没有,他会自动添加一个key的项,再把value赋值为相应的初始值(value是int的话赋值为0,string的话赋值为空).所以如果是插入的话可以用insert,如果是查找的话可以使用find,这样可以节省开销.查找的时间复杂度为O(logn) 2. 代码: #include<iostream> #include<string> #include<map> using namespace std; stri

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

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’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 exactl

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