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\n",sea(pat));
    }

    return 0;
}
时间: 2024-08-29 09:21:30

hdu 1251 字典树模板题 ---多串 查找单词出现次数的相关文章

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

HDU 1251 字典树入门

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 17177    Accepted Submission(s): 7410 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前

HDU 1251 统计难题(字典树模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 7 int num = 0; 8 9 struct Tr

hdu 1251(字典树)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 29183    Accepted Submission(s): 11454 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的

HDU 1251 统计难题(字典树模板题 || map运用)

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

hdu 1251 字典树的应用

这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include <cstring> #include <string> using namespace std; int main() { int i, len; char str[10]; map<string, int> m; while( gets(str) ) { len = strle

HDU 1166 线段树模板题

坐了3天的火车,终于到外婆家了(┬_┬).这次北京之旅颇有感触,虽然学到的东西不是很多(主要是自己的原因,没有认真学,并不是老师讲的不好),不过出去见见世面也是好的.最后一场比赛印象颇深,被虐的有点惨....记得当时有道题感觉自己能过,想用数组模拟链表水过,可是无论怎么优化一直超时@[email protected]后面比完后听朋友说那题应该用线段树做,顿时恍然大悟,然并卵,线段树的模板早忘了.....今天做道线段树的模板题先复习一下,过会再想想那道题. 敌兵布阵 Time Limit: 200

(模板)hdoj1251(字典树模板题)

题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树,从入门题开始.用数组实现,count数组表示每个结点出现次数,trie[0]为根节点.插入和查询一个字符串的复杂度为O(len).len为字符串的长度. AC code: #include<cstdio> #include<algorithm> #include<cstring&