Educational Codeforces Round 112 (Rated for Div. 2)-D. Say No to Palindromes-题解

71 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

@TOC

Educational Codeforces Round 112 (Rated for Div. 2)-D. Say No to Palindromes

传送门 Time Limit: 2 seconds Memory Limit: 256 megabytes

Problem Description

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 lil_i-th to rir_i-th position, inclusive.

Input

The first line contains two integers nn and mm (1n,m21051 \le n, m \le 2 \cdot 10^5) — 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 lil_i and rir_i (1lirin1 \le l_i \le r_i \le 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 lil_i-th to rir_i-th position, inclusive.

Sample Input

5 4
baacb
1 3
1 5
4 5
2 3

Sample Onput

1
2
0
1

Note

Consider the queries of the example test.

  • in the first query, the substring is baabaa, which can be changed to bacbac in one operation;

  • in the second query, the substring is baacbbaacb, which can be changed to cbacbcbacb in two operations;

  • in the third query, the substring is cbcb, which can be left unchanged;

  • in the fourth query, the substring is aaaa, which can be changed to baba in one operation.


题目大意

给你一个长度为nn的字符串和mm次询问,每次询问llrr的子串至少修改几个元素使得这个子串中的所有长度大于1的子串都不是回文串。

解题思路

这道题字符串中只能出现33种字母a,b,ca,b,c。要想所有子串中没有回文串,首先相邻两个不能相同,因为aaaa就是回文串。

所以不妨假设aa的左边是bb。那么aa的右边必须是cc。否则babbabbaabaa都包含回文串。

也就是说,aa旁边必须有bbcc,即任意连续33个元素必须取遍abcabc

所以最终不含回文串的长串长这样:abcabcabc...

这样的串有几种呢?3!=63!=6种。所以要修改的最终目标只有66种合法情况。对于每种合法情况,我们可以预知哪一位需要更改。像上题一样使用前缀数组即可快速得到llrr的元素一共需要修改几个。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char s[200010]; // 字符串
int qianZui[6][200010]; // 一共6种情况的前缀数组
void debug(int a[], int n) // debug用
{
    for(int i=1;i<=n;i++)
        cout<<a[i];
    puts("");
}
int main()
{
    int n,m;
    cin>>n>>m;
    scanf("%s",s+1); // 因为使用前缀,故从1开始输入
    fi(i,0,6)qianZui[i][0]=0; // 初始化前缀数组
    for(int diff=0;diff<3;diff++) // 这样能生成abc、bca、cab
    {
        for(int i=1;i<=n;i++)
        {
            char shouldBe=(i+diff)%3+'a';
            qianZui[diff][i]=qianZui[diff][i-1]+(s[i]!=shouldBe);
            // putchar(shouldBe);//**********
        }
        // puts("");//*******
        // debug(qianZui[diff],n);//**********
    }
    for(int diff=3;diff<6;diff++) // 这样能生成cba、bac、acb
    {
        for(int i=1;i<=n;i++)
        {
            char shouldBe='c'-(i+diff)%3;
            qianZui[diff][i]=qianZui[diff][i-1]+(s[i]!=shouldBe);
            // putchar(shouldBe);//**********
        }
        // puts("");//*******
        // debug(qianZui[diff],n);//**********
    }
    while(m--) // m次询问
    {
        int l,r;
        cd(l),cd(r); //scanf
        int m=10000000; // 初始值很大
        for(int i=0;i<6;i++) // 6种合法数组快速求得分别需要修改几个元素
            m=min(m, qianZui[i][r]-qianZui[i][l-1]);
        printf("%d\n",m); // 输出
    }
    return 0;
}

同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…