D. Say No to Palindromes
很牛逼
我们手动模拟一下可以知道只有3个字母不构成回文串只有可能是这样的
abcabc....
acbacb....
...
6种情况
所以直接暴力预处理即可
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 20220911;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
int sum[6][N],n,m;
string s;
void work(int a,int b,int c,int x){
for(int i=1;i<=n;i++){
if(i%3==0&&s[i]!=a)sum[x][i]++;
if(i%3==1&&s[i]!=b)sum[x][i]++;
if(i%3==2&&s[i]!=c)sum[x][i]++;
}
for(int i=1;i<=n;i++)sum[x][i]+=sum[x][i-1];
}
void solve() {
cin>>n>>m>>s;
s=")"+s;
work('a','b','c',0);
work('a','c','b',1);
work('b','a','c',2);
work('b','c','a',3);
work('c','a','b',4);
work('c','b','a',5);
while(m--){
int l,r;cin>>l>>r;
int ans=INF;
for(int i=0;i<6;i++){
ans=min(ans,sum[i][r]-sum[i][l-1]);
}
cout<<ans<<endl;
}
}
signed main(){
fast
int t;t=1;//cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}
标签:Educational,const,int,work,Codeforces,112
From: https://www.cnblogs.com/ycllz/p/16800230.html