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;
    }
     
  • 相关阅读:
    sql server 分组,取每组的前几行数据
    安装vim的ycm
    Linux下管道重定向使用以及Shell编程(操作系统)
    VirtualBox安装及Linux基本操作(操作系统实验一)
    创建表并查看表(数据库实验一)
    SQL SERVER安装(2008)
    ADT图及图的实现及图的应用
    并查集实现及使用
    堆及堆的应用/单调队列/堆排序
    AVL树/线索二叉树
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119726.html
Copyright © 2011-2022 走看看