Description
给出两个字符串 x x 和,有两种操作:
C i ch: C i c h : 把 x x 字符串第个字符改成 ch c h
Q L R: Q L R : 查询 x x 字符串区间中, y y 字符串出现了多少次
Input
第一行一整数表示用例组数,每组用例首先输入一整数 q q 表示操作数,然后输入两个字符串,最后 q q 行每行一个操作
Output
对于每个查询操作输出查询结果
Sample Input
1
5
AABBABA
AA
Q 1 3
C 6 A
Q 2 7
C 2 B
Q 1 5
Sample Output
1
2
0
Solution
因为 y y 的长度很小,可以暴力从的每个位置去匹配 y y ,匹配成功为,不成功为 0 0 ,用线段树维护匹配数的区间和,每次修改操作至多只会影响个匹配,分别单点更新即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
int num[maxn<<2],T,n,m,q;
char x[maxn],y[15];
#define ls (t<<1)
#define rs ((t<<1)|1)
void push_up(int t)
{
num[t]=num[ls]+num[rs];
}
void build(int l,int r,int t)
{
num[t]=0;
if(l==r)
{
if(l+m-1<=n)
{
int flag=1;
for(int i=0;i<m;i++)
if(x[l+i]!=y[i+1])
{
flag=0;
break;
}
num[t]=flag;
}
return ;
}
int mid=(l+r)/2;
build(l,mid,ls),build(mid+1,r,rs);
push_up(t);
}
void update(int z,int l,int r,int t,int v)
{
if(l==r)
{
num[t]=v;
return ;
}
int mid=(l+r)/2;
if(z<=mid)update(z,l,mid,ls,v);
else update(z,mid+1,r,rs,v);
push_up(t);
}
int query(int L,int R,int l,int r,int t)
{
if(L<=l&&r<=R)return num[t];
int mid=(l+r)/2,ans=0;
if(L<=mid)ans+=query(L,R,l,mid,ls);
if(R>mid)ans+=query(L,R,mid+1,r,rs);
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&q);
scanf("%s%s",x+1,y+1);
n=strlen(x+1),m=strlen(y+1);
build(1,n,1);
while(q--)
{
char op[3],ch[3];
int L,R;
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d%d",&L,&R);
if(R-L+1>=m)printf("%d\n",query(L,R-m+1,1,n,1));
else printf("0\n");
}
else
{
scanf("%d%s",&L,ch);
x[L]=ch[0];
for(int i=max(L-m+1,1);i<=min(L,n-m+1);i++)
{
int flag=1;
for(int j=0;j<m;j++)
if(x[i+j]!=y[j+1])
{
flag=0;
break;
}
update(i,1,n,1,flag);
}
}
}
printf("\n");
}
return 0;
}