Let's call the string beautiful if it does not contain a substring of length at least 22, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 33 letters of the Latin alphabet (in lowercase).
You are given a string ss of length nn, each character of the string is one of the first 33 letters of the Latin alphabet (in lowercase).
You have to answer mm queries — calculate the cost of the substring of the string ss from l_ili-th to r_iri-th position, inclusive.
Input
The first line contains two integers nn and mm (1 \le n, m \le 2 \cdot 10^51≤n,m≤2⋅105) — the length of the string ss and the number of queries.
The second line contains the string ss, it consists of nn characters, each character one of the first 33 Latin letters.
The following mm lines contain two integers l_ili and r_iri (1 \le l_i \le r_i \le n1≤li≤ri≤n) — parameters of the ii-th query.
Output
For each query, print a single integer — the cost of the substring of the string ss from l_ili-th to r_iri-th position, inclusive.
给定一个字符串(只有a,b,c三个字符) 把它变为无回文的字符串(单个字符不算回文)
因为只有a,b,c三个字符把a,b,c组合起来有六种情况 abc,acb,bac,bca,cab,cba
如果把abc和acb组合起来发现abcacb里面有回文所以abcacb不能组合
同理发现abc只能和abc组合这样才不会有回文
所以答案串是abcabcabc.......这样的形式
例 baacb 和 abcab 的同位字符进行比较 有四个字符不一样 题目要求求最少次数
baacb 和 cbacb 的差异字符最少 为二 就是答案
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int dp[6][MAXN];
string str[6] = {"abc","acb","bac","bca","cab","cba"};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,m,l,r;
cin >> n >> m;
string s;
cin >> s;
for(int i = 0;i < 6;i++){ // dp初始化
for(int j = 1;j <= n;j++){
dp[i][j] = dp[i][j-1] + (s[j-1] != str[i][(j-1) % 3]);
}
}
for(int k = 0;k < m;k++){
cin >> l >> r;
int Min = INT_MAX;
for(int i = 0;i < 6;i++){
int temp = dp[i][r] - dp[i][l-1];
Min = min(Min,temp);
}
cout << Min << "\n";
}
return 0;
}