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): 12577 Accepted Submission (s): 4481


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

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1247

The dictionary tree explains the link (the explanation is really good):

Http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html


Test instructions

Enter multiple strings, one row for each string, a set of test data, and a string to end the input with "CTRL + Z" . Determine if there are two strings in the input string combined into a string, that is, these 3 strings are the string just entered, guaranteeing the absolute existence of such a string.


Analysis

The dictionary tree is formed by creating or adding to the nodes of the tree the input string. Use the String function strncpy (a,x,y) to split the a array starting at coordinates x, including the y characters after coordinates x, and then use the dictionary tree to find out if there is a corresponding string. The problem is an exact match, and the split string cannot be part of a string of a dictionary tree, and must be the whole, that is, the last to reach the leaf node.


Code

#include <cstdio> #include <cstring> #include <malloc.h> #include <iostream>using namespace std  ; #define MAXN 26char str[50017][117];typedef struct trie{int v;//change as needed Trie *NEXT[MAXN]; Next is to indicate how many kinds of each layer of number, if only lowercase letters, then 26,//If the size of the letter, then 52, if plus the number, it is 62}trie;  Trie *root;//function function: Saves the input string to the dictionary tree void Createtrie (char *str) {int len = strlen (str);  Trie *p = root, *q;    for (int i = 0; i < len; i++) {int id = str[i]-' a '; Converting a character type to an integer can be converted by simply changing the defined struct int to char if (p->next[id] = = NULL)//If there is no node for this character {q = (Trie *) malloc (size   Of (Trie));   Create a new node q->v = 1; Initial v==1, not important, as long as the leaf node is the same as the line, the leaf node represents an end condition, so to be different from the other, no other requirements for (int j = 0; J < Maxn, J + +) Q->next[j            ] = NULL;            P->next[id] = q;    p = p->next[id];       Linked list Join Node} else {p = p->next[id];  If this layer has this character, use this node to execute the next layer}} p->v =-1; At the end, the V is changed to 1 to determine the end condition, so mark the}//function: Find the split string, whether the dictionary tree contains an int findtrie (Char *str) {int len = strlen (str);  Trie *p = root;    for (int i = 0; i < len; i++) {int id = str[i]-' a ';        Converts a character to a number p = p->next[id];   Points to the next layer node if (p = = NULL)//If it is an empty set, the string return 0 with this prefix is not saved;  if (p->v = =-1)//The string in the character set is the prefix//return-1 of this string;   }//return-1;  This string is the prefix of a string in the character set if (P->v = =-1)//description is an exact match, that is, the string containing the split, not the split string is part of the string in the tree return-1; else return 0;}    int main () {root = (Trie *) malloc (sizeof (Trie));       Open space for (int i = 0; i < MAXN; i++) root->next[i] = NULL;  Place 0 int cont = 0;      while (scanf ("%s", Str[cont])!=eof)//has been entered, even if the line break is not finished, there is only one set of tests {Createtrie (Str[cont]);                     Establish tree cont++;  Record how many strings} char a[117], b[117];   for (int i = 0; i < cont; i++)//split each string to see if it is a combination of the other two strings {int len = strlen (Str[i]);            The length of the string for (int j = 1; j < Len; j + +)//1.1-point split into two strings {memset (A, ' n ', sizeof (a));            memset (b, ' n ', sizeof (b)); StrncpY (a,str[i],j);      Separates the string into two strings, with a large number of loops strncpy (B,STR[I]+J,LEN-J);        if (Findtrie (a) ==-1 && Findtrie (b) ==-1)//If a split two string is found in the tree, which is equal to-1, the output {printf ("%s\n", Str[i]);      Break }}} return 0;}


Pass the Great god if you find a bug, be sure to leave a message.


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.