b i t s e t 字 符 串 匹 配

从 APJifengc 老师博客里第一次看见这个技术。然后今天翻 Alex_Wei 的博客又看到了,于是来学习一下。

嗯。 就这个东西,CF914F

经典例题

正解貌似是什么爆炸难写的分块SAM。

6s 1e5,果断乱搞。

这玩意的原理很简单,对模式串的每个字符开个bitset表示每个字符在哪个位置。查询的时候直接枚举文本串的每个字符,左移一定距离,把答案集合和字符集与一下就行了。

(于是我们就以791B的码量水了一道*3000)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
char s[100010],t[100010];
bitset<100010>S[26],ans;
int n,q;
int main(){
    scanf("%s%d",s,&q);
    n=strlen(s);
    for(int i=0;i<n;i++)S[s[i]-'a'][i]=1;
    while(q--){
        int od;scanf("%d",&od);
        if(od==1){
            int pos;
            scanf("%d%s",&pos,t);pos--;
            S[s[pos]-'a'][pos]=0;S[t[0]-'a'][pos]=1;
            s[pos]=t[0];
        }
        else{
            int l,r;scanf("%d%d%s",&l,&r,t);l--,r--;
            ans.set();
            int len=strlen(t);
            for(int i=0;i<len;i++)ans&=S[t[i]-'a']<<(len-i-1);
            printf("%d\n",max(0,(int)((ans>>(l+len-1)).count()-(ans>>(r+1)).count())));
        }
    }
    return 0;
}
posted @ 2022-11-20 20:56  gtm1514  阅读(78)  评论(0编辑  收藏  举报