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

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 112 (Rated for Div. 2)-D. Say No to Palindromes-题解相关的知识,希望对你有一定的参考价值。

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 2 2 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 3 3 letters of the Latin alphabet (in lowercase).

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

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

Input

The first line contains two integers n n n and m m m ( 1 ≤ n , m ≤ 2 ⋅ 1 0 5 1 \\le n, m \\le 2 \\cdot 10^5 1n,m2105) — the length of the string s s s and the number of queries.

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

The following m m m lines contain two integers l i l_i li and r i r_i ri ( 1 ≤ l i ≤ r i ≤ n 1 \\le l_i \\le r_i \\le n 1lirin) — parameters of the i i i-th query.

Output

For each query, print a single integer — the cost of the substring of the string s s s from l i l_i li-th to r i r_i ri-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 b a a baa baa, which can be changed to b a c bac bac in one operation;

  • in the second query, the substring is b a a c b baacb baacb, which can be changed to c b a c b cbacb cbacb in two operations;

  • in the third query, the substring is c b cb cb, which can be left unchanged;

  • in the fourth query, the substring is a a aa aa, which can be changed to b a ba ba in one operation.


题目大意

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

解题思路

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

所以不妨假设 a a a的左边是 b b b。那么 a a a的右边必须是 c c c。否则 b a b bab bab b a a baa baa都包含回文串。

也就是说, a a a旁边必须有 b b b c c c,即任意连续 3 3 3个元素必须取遍 a b c abc abc

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

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


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;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119272908

以上是关于Educational Codeforces Round 112 (Rated for Div. 2)-D. Say No to Palindromes-题解的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27