Hat's words (Dictionary tree)

Source: Internet
Author: User
Problem descriptiona hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are 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 will be no more than 50,000 words.
Only one case.
 
Outputyour output shoshould contain all the hat's words, one per line, in alphabetical order.
Sample Input
aahathathatwordhzieeword
 
Sample output
ahathatword
 

The question is to give you some words. You need to find the words that can be composed of the other two words. There are more than 50000 pieces of data, and the dictionary tree


# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; typedef struct trie {int flag; trie * Next [26];} trie; trie * root; int flag; char a [50000] [50]; void Inti () // initialization {root = (trie *) malloc (sizeof (trie )); root-> flag = 0; For (INT I = 0; I <26; I ++) Root-> next [I] = NULL;} void Charu (char *) // create a dictionary tree to insert the word {trie * P = root, * q; int L = strlen (a); For (INT I = 0; I <L; I ++) {int id = A [I]-'A'; If (p-> next [ID] = NULL) {q = (trie *) malloc (sizeof (trie); q-> flag = 0; For (INT I = 0; I <26; I ++) q-> next [I] = NULL; p-> next [ID] = Q;}/* else if (p-> next [ID]-> flag) {flag = 1 ;} */P = p-> next [ID];} p-> flag = 1;} int CZ (char * A) // find the word {trie * P = root; int Len = strlen (a); For (INT I = 0; I <Len; I ++) {int id = A [I]-'A '; P = p-> next [ID]; If (P = NULL) return 0;} return p-> Fla G;} int SF (trie * P) // you can skip this topic {// trie * P = root; If (P = NULL) return 0; for (INT I = 0; I <26; I ++) {If (p-> next [I]! = NULL) SF (p-> next [I]);} Free (p); Return 0;} int main () {Inti (); int n = 0; while (~ Scanf ("% s", & A [n]) {// flag = 0; // printf ("% s", a [I]); charu (A [n]); N ++;} For (INT I = 0; I <n; I ++) {int Len = strlen (A [I]); for (Int J = 0; j <Len; j ++) {char str1 [50] = {'\ 0 '}, str2 [50] = {'\ 0'}; // create two temporary arrays strncpy (str1, a [I], J); // split the word I, put strncpy (str2, a [I] + J, len-j) in two temporary arrays respectively; If (CZ (str1) & CZ (str2 )) // search through the search function. If both returns are 1, {printf ("% s \ n", a [I]); break;} can be formed ;}}} SF (Root); // release the memory (this question can be left blank) return 0 ;}


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.