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;
    }
     
  • 相关阅读:
    python网上开发执行环境
    bt5全称是Back Track five,是继BT3,BT4之后的最新版,这是一个linux环境的便携系统,可以放到U盘或者硬盘中启动,对本身硬盘没有影响,无需在本地安装。
    ubuntu安装mysql的步骤和配置总结
    Django 安装MySQLdb模块
    OpenCV3编程入门笔记(一)
    论文笔记---Deblurring Shaken and Partially Saturated Images
    win7 64位操作系统 电脑桌面出现this computer is being attacked的窗口
    论文笔记(一)---翻译 Rich feature hierarchies for accurate object detection and semantic segmentation
    OpenCV3计算机视觉Python语言实现笔记(五)
    OpenCV3计算机视觉Python语言实现笔记(四)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119726.html
Copyright © 2011-2022 走看看