题目: 给定一个字符串,长为 n,每次输入两个数l,r和2个字符a,b 问字符串里有多上对a,b
比如说
aba
问 ab
有1 2 ,但2 3、 2 1不算
#include<bits/stdc++.h>
using namespace std;
int n,q;
string S,T;
const int MN=2e5+5;
vector<int>pos[26],sum[26][26];
int f[MN][26];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
cin>>S;
for(int i=1; i<=n; i++) {
int t=(int)(S[i-1]-'a');
for(int x=0; x<26; x++){
f[i][x]=f[i-1][x];
sum[t][x].push_back(f[i][x]);
}
pos[t].push_back(i);
f[i][t]++;
}
for(int t=0; t<26; t++) {
for(int x=0; x<26; x++) {
for(int j=1; j<sum[t][x].size(); j++){
sum[t][x][j]+=sum[t][x][j-1];
}
}
}
while(q--) {
int l,r;
cin>>l>>r>>T;
int a=T[0]-'a',b=T[1]-'a';
int ql=lower_bound(pos[b].begin(),pos[b].end(),l)-pos[b].begin();
int qr=upper_bound(pos[b].begin(),pos[b].end(),r)-pos[b].begin()-1;
if(ql>qr) {
printf("0\n");
continue;
}
int cnt=qr-ql+1;
printf("%d\n",sum[b][a][qr]-(ql==0?0:sum[b][a][ql-1])
-f[l-1][a]*cnt);
}
return 0;
}