zoukankan      html  css  js  c++  java
  • HDU1247(经典字典树)

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 11894    Accepted Submission(s): 4239


    Problem Description
    A 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.
     
    Input
    Standard 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.
     
    Output
    Your output should contain all the hat’s words, one per line, in alphabetical order.
     
    Sample Input
    a
    ahat
    hat
    hatword
    hziee
    word
     
    Sample Output
    ahat
    hatword
     题意:‘帽子’单词:由其他两个单词拼接而成。输出所有的帽子单词。字典树。
    静态建树:
    #include"cstdio"
    #include"cstring"
    using namespace std;
    const int MAXN=50005;
    const int N=26;
    struct node{
        bool val;
        node* next[N];
    };
    node* root;
    node memory[MAXN];
    int ant;
    
    node* create()
    {
        node* p=&memory[ant++];
        for(int i=0;i<N;i++)
        {
            p->next[i]=NULL;
            p->val=false;
        }
        return p;
    }
    
    void insert(char *s)
    {
        node* p=root;
        for(int i=0;s[i];i++)
        {
            int k=s[i]-'a';
            if(p->next[k]==NULL) p->next[k]=create();
            p=p->next[k];
        }
        p->val=true;//若能走到最末端,则返回true; 
    }
    
    bool search(char *s)
    {
        node* p=root;
        for(int i=0;s[i];i++)
        {
            int k=s[i]-'a';
            if(p->next[k]==NULL)    return false;
            p=p->next[k];
        }
        return p->val;//若只是某个已插入单词的前缀,则返回false; 
    }
    
    int main()
    {
        int cnt=0;
        root=create();
        char word[MAXN][20];
        while(scanf("%s",word[cnt])!=EOF)
        {
            //if(word[cnt][0]=='0')    break;
            insert(word[cnt]);
            cnt++;
        }
        for(int i=0;i<cnt;i++)
        {
            for(int j=1;word[i][j];j++)
            {
                char fr[20]={''};
                char re[20]={''};
                strncpy(fr,word[i],j);
                strncpy(re,word[i]+j,strlen(word[i])-j);
                if(search(fr)&&search(re))
                {
                    printf("%s
    ",word[i]);
                    break;
                }
            }
        }
        
        return 0;
    }

    动态建树:

    #include"cstdio"
    #include"cstring"
    #include"cstdlib"
    using namespace std;
    const int MAXN=50005;
    const int N=26;
    struct Node{
        bool x;
        Node* next[N];
        Node()
        {
            x=false;
            for(int i=0;i<N;i++)    next[i]=NULL;
        }
    };
    char word[MAXN][20];
    int cnt;
    Node *root;
    void Insert(char s[])
    {
        Node *p=root;
        for(int i=0;s[i];i++)
        {
            int k=s[i]-'a';
            if(p->next[k]==NULL)    p->next[k]=new Node();
            p=p->next[k];
        }
        p->x=true;
    }
    bool Search(char s[])
    {
        Node *p=root;
        for(int i=0;s[i];i++)
        {
            int k=s[i]-'a';
            if(p->next[k]==NULL)    return false;
            p=p->next[k];
        }
        return p->x;
    }
    void Del(Node *p)
    {
        for(int i=0;i<N;i++)
        {
            if(p->next[i]!=NULL)
            {
                Del(p->next[i]);
            }
        }
        delete p;
    }
    int main()
    {
        root=new Node();
        while(scanf("%s",word[cnt])!=EOF)
        {
            //if(word[cnt][0]=='0')    break;
            Insert(word[cnt]);
            cnt++;
        }
        for(int i=0;i<cnt;i++)
        {
            for(int j=1;word[i][j+1];j++)
            {
                char fr[20]={''};
                char re[20]={''};
                strncpy(fr,word[i],j);
                strncpy(re,word[i]+j,strlen(word[i])-j);
                if(Search(fr)&&Search(re))
                {
                    printf("%s
    ",word[i]);
                    break;
                }        
            }
        }
        Del(root);
        return 0;
    }
     
  • 相关阅读:
    BibTex (.bib) 文件的凝视
    SQL注入原理解说,非常不错!
    怎样将文件隐藏在图片中
    白话经典算法系列之五 归并排序的实现
    帮你理解多线程
    很好的理解遗传算法的样例
    薏米红豆粥功效及做法介绍
    Linux makefile 教程 很具体,且易懂
    站点权重对于站点的重要性
    Codeforces Round #250 (Div. 2)——The Child and Set
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119726.html
Copyright © 2011-2022 走看看