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;
    }
     
  • 相关阅读:
    OWIN katana注册中间件的几种写法
    ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括
    entity framework 查询
    Sencha CMD 4- 安装与首次使用
    比较const ,readonly, stitac readonly
    (二)给IE6-IE9中的input添加HTML5新属性-placeholder
    (一)IE8以下background不起作用
    大虾翻译(一):jQuery.extend()
    JavaScript之三:jQuery插件开发(一)
    《JavaScript DOM编程艺术》
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119726.html
Copyright © 2011-2022 走看看