cf914F(字符串)

题目链接

bitset用法

搁置了好几天,终于想通......

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<bitset>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn];
int q;
int cnt;
bitset<maxn> m[27], ans;


int main()
{
    scanf("%s", s);
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        m[s[i] - 'a'][i + 1] = 1;
    }
    scanf("%d", &q);
    int op;
    char tmp[maxn];
    for(int i = 0; i < q; i++)
    {
        scanf("%d", &op);
        if(op == 1)
        {
            int pos;
            char ch[5];
            scanf("%d%s", &pos, ch);
            char tt = ch[0];
            m[s[pos - 1] - 'a'][pos] = 0;
            m[tt - 'a'][pos] = 1;
            s[pos - 1] = tt;
        }
        else
        {
            cnt = 0;
            ans.set();        //全部位置1
            int l, r;
            scanf("%d%d%s", &l, &r, tmp);
            int len = strlen(tmp);

            for(int i = 0; i < len; i++)
            {
                ans &= m[tmp[i] - 'a'] >> i;        //移动,利用相对位置拼出子串,如果ans[i] = 1表示以i开头长度为len(子串长度)的区间内有一个符合的解
            }

            int left = (ans >> l).count();
            int right = (ans >> (r - len + 1 + 1)).count();     //根据ans[i]的含义,实际范围变为l ~ r - len + 1(包括边界),查看这个区间内1的个数,所以分别右移做差,因为bitset起始位为0,所以右移(x + 1), x为l 或者 r - len + 1
            if(left < right)    left = right;
            printf("%d\n", left - right);
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值