zoukankan      html  css  js  c++  java
  • HDU1251统计难题---Trie Tree

    map巧过

    #include <stdio.h>
    #include <string.h>
    #include <map>
    #include <string>
    using namespace std;
    map<string,int>m1;
    int main()
    {
        char z[10];
        while(gets(z))
        {
            if(strlen(z)==0)
                break;
            for(int i=strlen(z);i>0;i--)
            {
                z[i]='';
                m1[z]++;
            }
        }
        while(gets(z))
        {
            printf("%d
    ",m1[z]);
        }
        return 0;
    }

     经典字典树(前缀树)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    
    using namespace std;
    
    struct node
    {
        int Count;///以此节点为前缀的单词数
        node *next[26];///26叉树
        node() ///初始化数据
        {
            for (int i=0;i<26;i++)
                next[i]=NULL;
            Count=0;
        }
    };
    
    node *p,*root=new node();
    void Insert(char *s)///插入新单词,即建立字典树
    {
        int i,k;
        p=root;
        for (i=0; s[i]!=''; i++)
        {
            k=s[i]-'a';
            if (p->next[k]==NULL)
                p->next[k]=new node();
        ///判断是不是新节点,如果是分配创建一个新节点来存贮 ,
        ///即root的next域对应的k位置是否为空
            p=p->next[k];///向前走一步
            p->Count++; ///以此位置为前缀的数量+1
        }
    }
    
    int Search(char *s)
    {
        int i,k;
        p=root;
        for (i=0; s[i]!=''; i++)
        {
            k=s[i]-'a';
            if (p->next[k]==NULL)///一旦查找不到,立即跳出
                return 0;
            p=p->next[k];
        }
        return p->Count;
    }
    
    int main()
    {
        char s[11];
        while (gets(s))
        {
            int len=strlen(s);
            if (len==0)
                break;
            Insert(s);
        }
        while (gets(s))
        {
            printf("%d
    ",Search(s));
        }
        return 0;
    }

    第一个字典树(G++内存超限),第二个map(红黑树),对于此类问题,字典树效率优势明显

    hihoCoder1014

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    
    using namespace std;
    
    struct node
    {
        int Count;///以此节点为前缀的单词数
        node *next[26];///26叉树
        node() ///初始化数据
        {
            for (int i=0;i<26;i++)
                next[i]=NULL;
            Count=0;
        }
    };
    
    node *p,*root=new node();
    void Insert(char *s)///插入新单词,即建立字典树
    {
        int i,k;
        p=root;
        for (i=0; s[i]!=''; i++)
        {
            k=s[i]-'a';
            if (p->next[k]==NULL)
                p->next[k]=new node();
        ///判断是不是新节点,如果是分配创建一个新节点来存贮 ,
        ///即root的next域对应的k位置是否为空
            p=p->next[k];///向前走一步
            p->Count++; ///以此位置为前缀的数量+1
        }
    }
    
    int Search(char *s)
    {
        int i,k;
        p=root;
        for (i=0; s[i]!=''; i++)
        {
            k=s[i]-'a';
            if (p->next[k]==NULL)///一旦查找不到,立即跳出
                return 0;
            p=p->next[k];
        }
        return p->Count;
    }
    
    int main()
    {
       // freopen("input.txt","r",stdin);
        char s[11];
        int n,m;
        scanf("%d",&n);getchar();
        while (n--)
        {
            gets(s);
            Insert(s);
        }
        scanf("%d",&m);
        getchar();
        while (m--)
        {
            gets(s);
            printf("%d
    ",Search(s));
        }
        return 0;
    }
  • 相关阅读:
    根据snort规则写openvas nasl 攻击 脚本
    snort规则中tcp/udp端口的具体作用
    snort规则头解析
    正则匹配中的特殊案例
    snort 规则 byte_test 不同运算符命中条件
    Linux中tar命令的一些用法
    Thymeleaf传递url参数
    PO BO VO DTO POJO DAO DO 令人迷惑的Java概念
    linux中多个命令连接符— ; && || ()
    遇见了count(1)这种写法,什么意思?
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6920147.html
Copyright © 2011-2022 走看看