• trie树模板(统计难题)


    统计难题

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
    Total Submission(s): 36675    Accepted Submission(s): 13637


    Problem Description
    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
     
    Input
    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

    注意:本题只有一组测试数据,处理到文件结束.
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
     
    banana
    band
    bee
    absolute
    acm
     
    ba
    b
    band
    abc
     
    Sample Output
    2 3 1 0
    Author
    Ignatius.L
     
    点一下查找树的要点:
    1.根节点是个空点,只有形式上的起点作用(插入、查找时从它开始)
    2.熟练应用指针,才能避免过多的调试
     

    指针型

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node{
        int k;bool ift;
        node *ch[26];
    }*root;
    node *create()
    {
        node *d=new(node);
        memset(d->ch,0,sizeof(d->ch));
        d->k=0;
        d->ift=0;
        return d;
    }
    void insert(char *s)
    {
        node *q=root;
        char *p=s;
        while(*p)
        {
            int id=*p-'a';
            if(q->ch[id]==NULL)q->ch[id]=create();
            q=q->ch[id];
            p++;
            q->k++;
        }
        q->ift=1;
    }
    int search(char *s)
    {
        node *q=root;
        char *p=s;
        while(*p)
        {
            int id=*p-'a';
            q=q->ch[id];
            p++;
            if(q==NULL)return 0;
        }
        return q->k;
    }
    int main()
    {
        root=create();char s[12];
        bool r=0;
        while(gets(s))
        {
            if(strlen(s)==0)break;
            insert(s);
        }
        while(gets(s))cout<<search(s)<<endl;
    }
    View Code

     数组模拟

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int trie[400001][26],len,root,tot,sum[400001];
    bool p;
    char s[11];
    void insert()
    {
        len=strlen(s);
        root=0;
        for(int i=0;i<len;i++)
        {
            int id=s[i]-'a';
            if(!trie[root][id]) trie[root][id]=++tot;
            sum[trie[root][id]]++;
            root=trie[root][id];
        }
    }
    int search()
    {
        root=0;
        len=strlen(s);
        for(int i=0;i<len;i++)
        {
            int id=s[i]-'a';
            if(!trie[root][id]) return 0;
            root=trie[root][id];
        }
        return sum[root];
    }
    int main()
    {
        while(gets(s))
        {
            if(!p)
            {
                if(strlen(s)) insert();
                else p=1;
            }
            else printf("%d
    ",search());
        }
    }
    View Code
  • 相关阅读:
    蚂蚁
    N的阶乘
    最小公倍数LCM
    最大公约数GCD
    Truck History(卡车历史)
    亲戚
    [SDOI2011]打地鼠
    连续自然数和
    P4250 [SCOI2015]小凸想跑步
    P4048 【[JSOI2010]冷冻波】
  • 原文地址:https://www.cnblogs.com/thmyl/p/6290891.html
Copyright © 2020-2023  润新知