Algorithm title: Hdu 1247 Hat ' s Words (dictionary tree)

Source: Internet
Author: User

Link:

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

The main effect of the topic:

By giving multiple words in a dictionary, you can see that some of the words are made up of the other two words in the dictionary. Output all words that match this condition in order.

Analysis and Summary:

Use the dictionary tree to save all the words, and then for all the words one by one enumerations, for each word, and then "split", split may have many situations, so enumerate the midpoint of the word split, in the dictionary order to find the split between the existence of two parts, the existence is output.

Code:

 #include <iostream> #include <cstdio> #include <cstring> #include <cctype> using Namespa  
     
CE std;  
const int MAXN = 2000000;  
     
const int KIND = 26;  
    struct node{bool Isword;  
    Node *next[kind];  
        void Init () {isword=false;  
    memset (Next, 0, sizeof (next));  
     
}}HEAP[MAXN];  
int cntnode,pos;  
     
Char word[50005][50];  
    Inline node* NewNode () {heap[cntnode].init ();  
Return &HEAP[cntNode++];  
    } void Insert (node *root, char *str) {int cnt=0;  
        for (char *p=str; *p; ++p) {int ch=*p-' a ';  
        if (root->next[ch]==null) {root->next[ch]=newnode ();  
    root = root->next[ch];  
} Root->isword = true;  
        BOOL Search (node *root, char *str) {for (char *p=str; *p; ++p) {int ch=*p-' a ';  
        if (root->next[ch]==null) return false;  
    root=root->next[ch]; } RETurn root->isword;  
    int main () {cntnode=0; pos=0;  
    Node *root=newnode ();  
    Char str[100];  
    while (gets (Word[pos])) {Insert (root, word[pos++]);  
        for (int i=0; i<pos; ++i) {if (word[i][0]==0 | | strlen (word[i)) ==1) continue;  
        memset (str, 0, sizeof (str));  
            for (int j=0; J<strlen (Word[i))-1; ++j) {str[j]=word[i][j];  
                if (search (ROOT,STR) && search (root,word[i]+j+1)) {puts (word[i]);  
            Break  
}} return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.