产生排列的方式
点击
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 2e5+10;
int n, m;
char a[N];
int s[6][N];
vector<string> neg;
void init()
{
neg.push_back("abc");
for(int i=0;i<6;i++)
{
auto s = neg.back();
next_permutation(s.begin(), s.end());
neg.push_back(s);
}
for(int i=0;i<6;i++)
{
for(int j=1;j<=n;j++)
{
s[i][j] = (a[j] != neg[i][j%3]) + s[i][j-1];
}
}
}
int main()
{
cin >> n >> m;
cin >> (a+1);
init();
while(m--)
{
int l, r, ans = n + 1;
cin >> l >> r;
for(int i = 0; i < 6; i++)
ans = min(ans, s[i][r] - s[i][l-1]);
printf("%d\n", ans);
}
}