Hdu-1247-hat ' s Words (dictionary tree!!) )

Source: Internet
Author: User

Hat ' s WordsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8579 Accepted Submission (s): 3090


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

Author hat.


Test instructions: Given some words (given in dictionary order), output all words that meet the criteria in dictionary order (conditional on the word being composed of two other words)

Idea: First construct a dictionary tree, and then then determine whether the word has the other two words,, the judgment method for the word in the existence of P->is to True point, and then put point +1 into the stack, to see if this point can form a other word, is the output, not the output.


AC Code:

#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, size (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) r Eturn 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;}  


Hdu-1247-hat ' s Words (dictionary tree!!) )

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.