Test instructions give you a dictionary all the words in the output dictionary that can be represented as two words connected
The most basic application of the dictionary tree is to add all the words to the dictionary tree to mark each node as the end of a word and find each word at the end of a word in the tree. If the remaining suffix is also a word, the currently queried word can be a two-word connection.
#include <cstdio> #include <cstring>using namespace std;const int N = 50005;int N;char ss[n][20];struct trie{ Trie *chi[26]; BOOL Isend; Trie () {isend = false; memset (chi, NULL, sizeof (CHI)); }}*root;void Inserttrie (Trie *r, char s[]) {int i = 0, J; while (S[i]) {j = s[i++]-' a '; if (r->chi[j] = = NULL) R->chi[j] = new Trie (); r = r->chi[j]; } R->isend = true;} OP flag This query is query suffix or query entire bool Searchtrie (Trie *r, char s[], int op) {int i = 0, J; while (S[i]) {j = s[i++]-' a '; if (r->chi[j] = = NULL) return false; r = r->chi[j]; if (!op && r->isend && s[i])//prefix is a word {if (Searchtrie (root, S + I, 1)) return true; is the query suffix a word}} return op? R->isend:op;} int main () {int m = 0; root = new Trie (); while (~SCANF ("%s", Ss[m]) Inserttrie (root, ss[m++]); for (int i = 0; I < m; ++i) if (Searchtrie (root, Ss[i], 0)) puts (ss[i]); return 0;}
Hat ' s Words
Problem Descriptiona Hat's word is a word in the dictionary that's the concatenation of exactly the other words in the DI Ctionary.
You're to find all the hat's words in a dictionary.
Inputstandard input consists of a number of lowercase words, one per line, in alphabetical order. There'll be is no more than 50,000 words.
Only one case.
Outputyour output should contain all the hat's words, one per line, in alphabetical order.
Sample Input
Aahathathatwordhzieeword
Sample Output
Ahathatword
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1247 Hat ' s Words (dictionary tree · Trie)