Question link:
Ultraviolet A: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 1332
Zoj: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 825
Type: Hash
Original question:
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.
Output
Your output shoshould contain all the compound words, one per line, in alphabetical order.
Sample Input
aalienbornlesslienneverneverthelessnewnewbornthezebra
Sample output
aliennewborn
Question:
The question is very short and concise ~!
It provides a series of words in Lexicographic Order and regards them as a dictionary. Then, all the compound words in the dictionary are output in Lexicographic Order. The compound word is composed of the other two words in the dictionary.
Ideas and summary:
Naturally, one idea is to enumerate each word in order and determine whether the word is a compound word.
The core of this question is how to determine whether a word is a compound word. by defining a compound word, you can split the word into two and enumerate all the sharding schemes of a word, then, you can determine whether there are two split words in the dictionary.
The question data volume reaches 120,000, so you must find a quick way to determine whether a word belongs to a dictionary set.
This is highlighted by the ultra-high speed of hash tables. You only need to create a hash table ing relationship for each word, and then you can determine whether a word belongs to the dictionary at almost O (1) time.
/** Ultraviolet A 10391-compound words * hash table * Time: 0.020 S (ultraviolet A) * Author: d_double */# include <iostream> # include <cstdio> # include <cstring> # define maxn 120003 using namespace STD; typedef char word [30]; WORD [maxn]; const int hashsize = maxn; int N, head [hashsize], next [hashsize]; inline void init_lookup_table () {n = 1; memset (Head, 0, sizeof (head);} inline int Hash (char * Str) {// string hash function int seed = 131; int hash = 0; while (* Str) hash = hash * seed + (* STR ++); Return (hash & 0x7fffffff) % hashsize;} int add_hash (int s) {int H = hash (word [s]); int u = head [H]; while (u) u = next [u]; next [s] = head [H]; head [H] = s; return 1;} int search (char * s) {int H = hash (s ); int u = head [H]; while (u) {If (strcmp (word [u], S) = 0) return U; u = next [u];} return 0;} int main () {word STR; n = 1; init_lookup_table (); While (gets (word [N]) {add_hash (N ); ++ n ;}// query for (INT I = 1; I <n; ++ I) if (word [I] [1]) {for (Int J = 0; j <strlen (word [I])-1; ++ J) {strcpy (STR, word [I]); STR [J + 1] = '\ 0'; If (search (STR) & search (word [I] + J + 1 )) {puts (word [I]); break ;}} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double(For details, refer)