题意:给你一个DNA串,有两种操作
1 a e:把位置a的字符换成字符e
2 a b e:问[ a, b ]的区间内eeeeee..(e循环串)与相同的的字符有几个,并输出。
思路:用线段树来保存每个字母在每个区间处于每种循环串位置的数量,
node[ 字母种类 ] [ e的长度 ] [ 长度取模(相对e的长度位置) ] [ 位置 ],然后就是基本的树状数组插入和查询
线段树T了,up里40的合并太大了,树状数组感觉是10
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
int node[4][11][11][maxn];
int h[110];
char s[maxn], e[11];
int lowbit(int x){
return x&(-x);
}
void add(int ty, int pos, int x, int d){ //ty:字母的种类 pos:相对位置 x:实际位置 d:变换的值
for(; x < maxn; x+=lowbit(x)){
for(int k = 1; k <= 10; k++){
node[ty][k][pos%k][x] += d;
}
}
}
int sum(int ty, int len,int pos, int x){ //ty:字母的种类 len:e子串的长度 pos:相对于e子串的位置 x:实际位置
int ans = 0;
for(; x > 0 ; x-=lowbit(x)){
ans += node[ty][len][pos][x];
}
return ans;
}
int query(int l,int r,int ty,int len,int pos){ //区间[l,r]
return sum(ty, len, pos, r) - sum(ty, len, pos, l-1);
}
int main(){
int n;
h['A'] = 0; h['T'] = 1; h['C'] = 2; h['G'] = 3; //给四个字母标记数字
scanf("%s%d",s,&n);
int len = strlen(s);
for(int i = 0;i < len; i++){
add(h[s[i]], i, i+1, 1); //因为树状数组是从1开始,则i+1为实际位置。但是相对位置之后要取模所以不能+1,就按照原来的位置取模完才能会是正确的相对位置
}
while(n--){
int a, b, c;
scanf("%d",&a);
if(a == 1){
scanf("%d%s",&b,e);
add(h[s[b-1]], b-1, b, -1); //把s[b-1]原来的字母的数量减去
add(h[e[0]], b-1, b, 1); //把新的字母的数量+1
s[b-1] = e[0];
}
else{
scanf("%d%d%s",&b,&c,e);
len = strlen(e);
int ans = 0;
for(int i = 0; i < len; i++){ //枚举每个e字符串的上的字符的数量。
ans += query(b, c, h[e[i]], len, (i + b -1)%len); //这里几个(i+b-1)要记得取模len,不然就凉了
}
printf("%d\n",ans);
}
}
return 0;
}
//T16
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
char ch[100005];
struct node
{
int l,r,sz;
int ct[4][11][10];
}tr[100005*3];
int f(char c)
{
if(c=='A')
return 0;
if(c=='T')
return 1;
if(c=='G')
return 2;
if(c=='C')
return 3;
}
void up(node& cur,node a,node b,int m)
{
cur.sz=(a.sz+b.sz);
cur.l=a.l;cur.r=b.r;
for(int t=0;t<4;t++)
for(int i=0;i<m;i++)
cur.ct[t][m][i]=0;
for(int t=0;t<4;t++)
{
for(int j=0;j<m;j++)
{
cur.ct[t][m][j]+=a.ct[t][m][j];
cur.ct[t][m][(j+a.sz)%m]+=b.ct[t][m][j];
}
}
}
void build(int i,int l,int r)
{
if(l==r)
{
tr[i].l=l;tr[i].r=r;tr[i].sz=1;
for(int t=0;t<4;t++)
for(int l=1;l<=10;l++)
for(int j=0;j<l;j++)
tr[i].ct[t][l][j]=0;
int fg=f(ch[l-1]);
for(int j=1;j<=10;j++)
{
tr[i].ct[fg][j][0]=1;
}
return;
}
else
{
int mid=(l+r)/2;
build(i*2,l,mid);
build(i*2+1,mid+1,r);
for(int j=1;j<=10;j++)
up(tr[i],tr[i*2],tr[i*2+1],j);
return;
}
}
void update(int i,int l,int r,int ql,int qr,int c)
{
if(l==ql && r==qr)
{
int pre=f(ch[l-1]);
for(int j=1;j<=10;j++)
tr[i].ct[pre][j][0]--,tr[i].ct[c][j][0]++;
}
else
{
int mid=(l+r)/2;
if(qr<=mid)
update(i*2,l,mid,ql,qr,c);
else if(ql>mid)
update(i*2+1,mid+1,r,ql,qr,c);
else
{
update(i*2,l,mid,ql,mid,c);
update(i*2+1,mid+1,r,mid+1,qr,c);
}
for(int j=1;j<=10;j++)
up(tr[i],tr[i*2],tr[i*2+1],j);
return;
}
}
node get(int i,int l,int r,int ql,int qr,int m)
{
if(l==ql && r==qr)
{
return tr[i];
}
else
{
int mid=(l+r)/2;
if(qr<=mid)
return get(i*2,l,mid,ql,qr,m);
else if(ql>mid)
return get(i*2+1,mid+1,r,ql,qr,m);
else
{
node ans;
node a=get(i*2,l,mid,ql,mid,m);
node b=get(i*2+1,mid+1,r,mid+1,qr,m);
up(ans,a,b,m);
return ans;
}
}
}
char tch[15];
int main()
{
while(~scanf("%s",ch))
{
int q;
scanf("%d",&q);
int len=strlen(ch);
build(1,1,len);
//cout<<tr[2].ct[0][10][0]<<endl;
while(q--)
{
int tp;
scanf("%d",&tp);
if(tp==1)
{
int id;
char tpc;
scanf("%d",&id);
getchar();
scanf("%c",&tpc);
int tf=f(tpc);
if(tpc!=ch[id-1])
update(1,1,len,id,id,tf);
ch[id-1]=tpc;
}
else if(tp==2)
{
int l,r;
scanf("%d%d",&l,&r);
scanf("%s",tch);
int tlen=strlen(tch);
node ans=get(1,1,len,l,r,tlen);
int anss=0;
for(int i=0;i<tlen;i++)
{
int tf=f(tch[i]);
anss+=ans.ct[tf][tlen][i];
}
printf("%d\n",anss);
}
}
}
}