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;
    }
     
  • 相关阅读:
    GPU CUDA之——深入理解threadIdx
    需求分析、业务逻辑与数据结构
    软件建模的本质
    浅谈软件需求建模
    软件建模即程序设计
    软件开发从0到1与软件建模
    数据模型所描述的内容包括三个部分:数据结构、数据操作、数据约束。
    观察力与信息搜集能力
    人类为什么写书
    鲁宾斯坦说:"思维是在概括中完成的。"
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119726.html
Copyright © 2011-2022 走看看