zoj 3228 trie tree(字典树)

本文介绍了一种高效的字符串搜索算法,该算法能够快速查找一个长字符串中多个短子串出现的次数,支持子串出现的重叠与非重叠两种情况。通过使用前缀树(Trie)的数据结构来实现高效搜索,特别适用于子串长度较短的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Searching the String

Time Limit: 7 Seconds      Memory Limit: 129872 KB

Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who can help me? I'll bg him! "

So what is the problem this time?

First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.

At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.

I know you're a good guy and will help with jay even without bg, won't you?

Input

Input consists of multiple cases( <= 20 ) and terminates with end of file.

For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output the case number first ( based on 1 , see Samples ).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.

Output an empty line after each case.

Sample Input

ab
2
0 ab
1 ab

abababac
2
0 aba
1 aba

abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn

Sample Output

Case 1
1
1

Case 2
3
2

Case 3
1
1
0

Hint

In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.

For C++ users, kindly use scanf to avoid TLE for huge inputs.


Author: LI, Jie
Source: ZOJ Monthly, July 2009

字符串可重复就直接是模板题

对于不可以重复的串,因为串长度<=6 所以可以用标记 和枚举1到6 的串

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n,flag;
int ans[N],
      f[N],
      pre[N],
      cnt = 0;

struct trie
{
    trie *next[26];
    int id;
    trie (){
    for(int i = 0; i < 26 ; ++i) next[i] = NULL;
    id = 0;
    }
}e[N*6];
void init(){
    memset(pre,0,sizeof(pre));
    memset(ans,0,sizeof(ans));

    for(int i = 0; i <= cnt; i++){
            e[i].id = 0 ;
            for(int j = 0 ; j < 26 ; j++) e[i].next[j] = NULL;
    }
      cnt = 0;
}
class Trie
{
    public :
    trie * root ;
    void init() {
    root = &e[cnt++];
    }
    void insert(char *s, int id){
        int len = strlen(s);
        trie *p = root;
        for(int i = 0 ; i < len ; ++i){
            int idx = s[i] - 'a';
            if(!p->next[idx]) p->next[idx] = &e[cnt++];
            p = p->next[idx];
        }
            p->id = id;
        }
    int query(char *s, int len){
        trie * p = root;
        for(int i = 0; i < len ; ++i){
            int id = s[i] - 'a';
            if(!p->next[id]) return 0;
            p = p->next[id];
        }
        return p->id;
    }


}T[2];
char s[N],str[N][10];
void run()
{   int cas = 1;
    while(~scanf("%s",s)){
        init();
        T[1].init();
        T[0].init();
        scanf("%d",&n);
        for(int i = 1; i <= n; ++i){
            scanf("%d%s",&flag,str[i]);
            T[flag].insert(str[i],i);
            f[i] = flag;
        }
        int len = strlen(s);
        for(int i = 0 ; i < len ; ++i){
            for(int x = 1; i + x <= len && x <= 6; ++x){
                flag = T[0].query(s + i, x);
                ans[flag]++;
                flag = T[1].query(s + i, x);
                if(flag && pre[flag] <= i){
                    ans[flag]++;
                    pre[flag] = x + i;
            }

            }
        }
        //
        printf("Case %d\n",cas++);
        for(int i = 1 ; i <= n; i ++){
            flag = f[i];
            int len = strlen(str[i]);
            int id =  T[flag].query(str[i],len);
            printf("%d\n",ans[id]);

        }
        puts("");
    }
}
int main()
{
   // freopen("in]","r",stdin);
    run();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值