HDU 1247 Hat ' s Words (dictionary tree · Trie)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.