zoukankan      html  css  js  c++  java
  • 统计难题---hdu1251字典树模板

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=1251

    node *head=(node*)malloc(sizeof(node));
    for(int i=0; i<26; i++)
    {
       head->next[i] = NULL;
       head->sum = 0;
    }
    可以改成node *head=new node();
    要用c++提交才对,不然G++就内存超限了-_-
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    #include<stdlib.h>
    using namespace std;
    
    struct node
    {
        int sum;
        node *next[26];
    };
    
    void BuildLibTree(node *head, char s[])
    {
        node *p = head,*q;
    
        for(int i=0; s[i]; i++)
        {
            int k = s[i] - 'a';
            if(p->next[k] == NULL)
            {
                q=(node*)malloc(sizeof(node));
                for(int j=0; j<26; j++)
                {
                    q->next[j] = NULL;
                    q->sum = 0;
                }
                p->next[k] = q;
            }
            p = p->next[k];
            p->sum++;
        }
    }
    int Query(node *head, char s[])
    {
        node *p = head;
        for(int i=0; s[i]; i++)
        {
            int k = s[i]-'a';
            if(p->next[k] == NULL)
                return 0;
            p = p->next[k];
        }
        return p->sum;
    }
    int main()
    {
        char s[20];
        node *head=(node*)malloc(sizeof(node));
        for(int i=0; i<26; i++)
        {
            head->next[i] = NULL;
            head->sum = 0;
        }
        while(gets(s),s[0])
            BuildLibTree(head, s);
    
        while(scanf("%s",s)!=EOF)
            printf("%d
    ", Query(head, s));
        return 0;
    }
  • 相关阅读:
    再谈树形dp
    洛谷 P3627 [APIO2009]抢掠计划
    树状数组
    树形dp 入门
    洛谷P2014 选课
    洛谷P2015 二叉苹果树
    9 vue.js 被观察数组的变异方法
    8 vue的v-model指令学习
    7vue-事件修饰符
    6.vue事件绑定-click
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4691267.html
Copyright © 2011-2022 走看看