cf Educational Codeforces Round D. Say No to Palindromes

原题:
D. Say No to Palindromes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let’s call the string beautiful if it does not contain a substring of length at least 2 , 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 3 letters of the Latin alphabet (in lowercase).

You are given a string s of length n, each character of the string is one of the first 3 letters of the Latin alphabet (in lowercase).

You have to answer m queries — calculate the cost of the substring of the string s from li-th to ri-th position, inclusive.

Input

The first line contains two integers n and m (1≤n,m≤2*10^5) — the length of the string s and the number of queries.

The second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters.

The following m lines contain two integers li and ri (1≤li≤ri≤n) — parameters of the i-th query.

Output
For each query, print a single integer — the cost of the substring of the string s from li-th to ri-th position, inclusive.

Example

Input
5 4
baacb
1 3
1 5
4 5
2 3

Output
1
2
0
1

Note

Consider the queries of the example test.
in the first query, the substring is baa, which can be changed to bac in one operation;
in the second query, the substring is baacb, which can be changed to cbacb in two operations;
in the third query, the substring is cb, which can be left unchanged;
in the fourth query, the substring is aa, which can be changed to ba in one operation.

中文:
如果一个字符串没有回文子串,则定义一个字符串beautiful。给你一个仅由abc三个字母组成的字符串,然后给你n个查询区间,让你判断将这个区间中的字符串变成beaufiful的最少花费代价。
把这个字符串中的某一个字符替换成另一个,则花费一个开销。最后总开销就是总共进行了多少次字符的替换使得这个区间的字符串是beaufiful。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<limits>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int n, m;
const string tpl[6] = { "abc","acb","bac","bca","cab","cba" };
int mark[6][maxn];
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n>> m) {
        string s;
        cin >>s;
        memset(mark,0, sizeof(mark));
        for(int i=0;i<6;i++) {
            for (int j=1;j<=n;j++) {
                if (tpl[i][(j-1)%3] != s[j-1]) {
                    mark[i][j] = mark[i][j-1] + 1;
                } else {
                    mark[i][j] = mark[i][j-1];
                }
            }
        }
        while(m--) {
            int l, r;
            cin >> l >> r;
            int ans = INT_MAX;
            for (int i=0;i<6;i++) {
                ans = min(ans, mark[i][r] - mark[i][l-1]);
            }
            cout << ans << endl;
        }
    }
    return 0;
}
 

解答:

三个字母组成的字符串,如果不存在回文子串,必然是三个字符进行全排列,然后循环出现的形式。
比如,acbacbacb这种
把所有6种情况枚举出来,然后与输入的字符串在给定的区间上进行比对,找出最小不同字符的个数就是最少替换的次数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值