CodeForces - 914F Substrings in a String

题意:

给定一个字符串 s s s q q q 次操作:① 1 , i , c 1, i, c 1,i,c,将 s i s_i si 改为 c c c;② 2 , l , r , t 2, l, r, t 2,l,r,t,求串 t t t s [ l ⋯ r ] s[l\cdots r] s[lr] 中出现的次数。 ( ∣ s ∣ ,   q ,   Σ ∣ t ∣ ≤ 1 0 5 ) (|s|,~q,~\Sigma |t| \leq 10^5) (s, q, Σt105)

链接:

https://codeforces.com/problemset/problem/914/F

解题思路:

可以根据串长分类,得到一个带根号的做法,即分块建立 S A M SAM SAM,串长大于 n \sqrt{n} n k m p kmp kmp 匹配,这一部分复杂度 O ( n n ) O(n\sqrt{n}) O(nn );否则整块跑 S A M SAM SAM,横跨整块的匹配依然用 k m p kmp kmp 来解决,散块部分也使用 k m p kmp kmp,串长 ∣ t ∣ |t| t,处理整块 O ( ∣ t ∣ n ) O(|t| \sqrt{n}) O(tn ),处理横跨整块的匹配,只需讨论横跨处 2 ∣ t ∣ 2|t| 2t 长的主串 O ( ∣ t ∣ n ) O(|t|\sqrt{n}) O(tn ),散块 O ( ∣ t ∣ ) O(|t|) O(t)。修改操作则暴力重建 S A M SAM SAM。那么总复杂度 O ( n n ) O(n\sqrt{n}) O(nn )

然而,牛客上做过这种题,我们可以有 O ( n 2 32 ) O(\frac{n^2}{32}) O(32n2) 的做法,用 b i t s e t bitset bitset 暴力匹配,即预处理串 s s s 每种字符出现的下标集合,当有匹配时, t i t_i ti s s s 中出现的下标必然是 t i − 1 t_{i - 1} ti1 s s s 中出现下标 + 1 +1 +1,那么令匹配下标初始化为 t 1 t_1 t1 s s s 中出现的下标集合,每次 & \& & 上对应的需要满足的下标集合即可。

参考代码:
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define sz(a) ((int)a.size())
#define pb push_back
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid (l + r >> 1)
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
 
char s[maxn], t[maxn];
bitset<maxn> bs[26], msk, ret;
int n, q;
 
int main(){
 
	ios::sync_with_stdio(0); cin.tie(0);
	msk.set();
	cin >> s;
	n = strlen(s);
	for(int i = 0; i < n; ++i){
 
		bs[s[i] - 'a'].set(i);
	}
	cin >> q;
	while(q--){
        
		int opt, x, y; cin >> opt >> x;
		if(opt == 1){
 
			cin >> t; --x;
			bs[s[x] - 'a'].reset(x);
			s[x] = t[0];
			bs[s[x] - 'a'].set(x);
		}
		else{
 
			cin >> y >> t; --x, --y;
			int m = strlen(t);
			int l = x, r = y - m + 1;
			if(l > r){
 
				cout << "0\n";
				continue;
			}
			ret = bs[t[0] - 'a'] & (msk << l) & (msk >> (maxn - r - 1));
			for(int i = 1; i < m; ++i){
 
				ret <<= 1;
				ret &= bs[t[i] - 'a'];
			}
			cout << ret.count() << "\n";
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值