zoukankan      html  css  js  c++  java
  • hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA

    然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~

    只是记录下这道题学到的方法吧:

        for(rt = 0; *s; rt = nxt, ++s)
        {
            nxt=tree[rt][*s-tb];
            if(!nxt)
            {
                nxt=tree[rt][*s-tb]=top;
                memset(tree[top],0,sizeof(tree[top]));
                top++;
            }
            tree[nxt][tk]++;//////////
        }
    //////////////处,假设改为tree[rt][tk]表示某个节点有几个孩子,tree[nxt][tk]++经过当前字母的单词个数


    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int tk=26;
    const int tb='a';
    const int MAXN= 500000;
    
    int top,tree[MAXN][tk+2];
    
    void init()
    {
        top=1;
        memset(tree[0],0,sizeof(tree[0]));
    }
    
    void Insert(char *s, int Rank=1)
    {
        int rt,nxt;
        for(rt = 0; *s; rt = nxt, ++s)
        {
            nxt=tree[rt][*s-tb];
            if(!nxt)
            {
                nxt=tree[rt][*s-tb]=top;
                memset(tree[top],0,sizeof(tree[top]));
                top++;
            }
            tree[nxt][tk]++;
        }
    }
    
    int sea(char *s)
    {
        int rt;
        for(rt=0;rt=tree[rt][*s-tb];)
        {
            if(*(++s)==0)return tree[rt][tk];
        }
        return 0;
    }
    
    char str[150],pat[150];
    
    int main()
    {
       //freopen("hdu1251.txt","r",stdin);
        char cc;
        int ans=0;
        init();
        while(gets(str)&&str[0])
        {
            Insert(str);
        }
        while(~scanf("%s",pat))
        {
            //cout << pat << endl;
            printf("%d
    ",sea(pat));
        }
    
        return 0;
    }
    


  • 相关阅读:
    自己实现的string的库函数
    单链表的面试题
    顺序表的实现
    指针数组与数组指针
    指针与数组
    sizeof 与 strlen
    HTML配色工具!在线配色工具
    [转载] python的sorted函数对字典按key排序和按value排序
    [转载]python脚本删除一定时间以外的文件
    python基础教程(四)
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4012902.html
Copyright © 2011-2022 走看看