Hdu Hat's Words (Dictionary tree)

Source: Internet
Author: User
Hat's WordsTime Limit: 2000/1000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other) Total Submission (s): 7 Accepted Submission (s): 4 Font: times New Roman | Verdana | GeorgiaFont Size: Regular → 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
Author determines whether a word can be composed of two other words. Note that other words may also consist of several words! Use the dictionary tree to query a word. When a word becomes obsolete, I search for the remaining part of the word. In this step, I use recursion to control the number of recursive layers with a count variable, but the code is messy and there is not much reference value.
# Include <stdio. h> # include <string. h> int cnt; int ant; char text [50005] [105]; struct Node {int next [26]; int flag;} tree [100000]; void Insert (char * s) {int p = 0; while (* s) {if (tree [p]. next [* s-'a'] =-1) {tree [p]. next [* s-'a'] = cnt; cnt ++;} p = tree [p]. next [* s-'a']; s ++;} tree [p]. flag = 1;} // use count as the flag to record the nth layer of int Query (char * s, int count) {int p = 0; while (* s) {if (tree [p]. next [* s-'a'] =-1 ){ Return 0;} p = tree [p]. next [* s-'a']; // when the dictionary word if (tree [p]. flag = 1 & * (s + 1) {// when the first word if (count = 1) {count ++; Query (s + 1, count); // search for the remaining words count = 1; // restore count to 1 and continue searching in the first recursion} // when searching for the remaining words, it is also possible that the remaining words are composed of more than two words // Therefore, You Need To separately determine else if (count = 2) {s ++; continue ;}} s ++ ;} if (tree [p]. flag = 1) {// a messy place... Do not tangle if (ant = 2) {return 0;} ant ++;} return 0;} int main () {int I, j; int len; cnt = 1; for (I = 0; I <100000; I ++) {memset (tree [I]. next,-1, sizeof (tree [I]. next); tree [I]. flag = 0;} I = 0; while (scanf ("% s", text [I])! = EOF) {Insert (text [I]); I ++;} len = I; for (j = 0; j <len; j ++) {ant = 0; // Query (text [j], 1); if (ant = 2) {printf ("% s \ n ", text [j]) ;}} 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.