zoukankan      html  css  js  c++  java
  • HDU 1251 统计难题(Trie模版题)

    统计难题

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

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

    注意:本题只有一组测试数据,处理到文件结束.
     
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
    banana
    band
    bee
    absolute
    acm
     
    ba
    b
    band
    abc
     
    Sample Output
    2 3 1 0
     

    题目链接:HDU 1251

    以前以为很高级的数据结构,原来就是一个很多后继节点的链表而已,建立过程很简单明了,不懂的话画一个多叉树就知道了……,每一次从表头*root(最近学的是链表,就用*L好了)开始找,然后如果存在就继续找直到遍历完字符串,插入和查找都是这个原理还有最好别用G++提交容易爆内存,C++就不会了

    代码:

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <sstream>
    #include <cstring>
    #include <bitset>
    #include <string>
    #include <deque>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=26;
    const int M=15;
    struct Trie
    {
        Trie *nxt[N];
        int cnt;
        Trie()
        {
            CLR(nxt,0);
            cnt=0;
        }
    };
    Trie *L=new Trie();
    void update(char s[])
    {
        int len=strlen(s);
        int indx;
        Trie *cur=L;
        for (int i=0; i<len; ++i)
        {
            indx=s[i]-'a';
            if(cur->nxt[indx])
            {
                cur=cur->nxt[indx];
                ++(cur->cnt);
            }
            else
            {
                Trie *one=new Trie();
                ++(one->cnt);
                cur->nxt[indx]=one;
    
                cur=cur->nxt[indx];///新建之后还是要跳到这个新建节点的
            }
        }
    }
    int Find(char s[])
    {
        int len=strlen(s);
        Trie *cur=L;
        int indx;
        for(int i=0; i<len; ++i)
        {
            indx=s[i]-'a';
            if(!cur->nxt[indx])
                return 0;
            cur=cur->nxt[indx];
        }
        return cur->cnt;
    }
    char s[M];
    int main(void)
    {
        while (gets(s)&&strcmp(s,""))
            update(s);
    
        while (gets(s))
            printf("%d
    ",Find(s));
        return 0;
    }
  • 相关阅读:
    Aop 打印参数日志时,出现参数序列化异常。It is illegal to call this method if the current request is not in asynchron
    Nginx 配置反向代理ip
    springboot 的启动流程
    sqlserver 查看表死锁
    idea 中 下载源码:Sources not download for:
    sqlserver exists 与 in 的区别
    sqlserver case when 的使用方法
    Java项目启动时执行指定方法的几种方式
    使用多线程并获取执行结果
    nodejs实现一个文件存储服务
  • 原文地址:https://www.cnblogs.com/Blackops/p/5904583.html
Copyright © 2011-2022 走看看