cf1555D. Say No to Palindromes

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1555D. Say No to Palindromes相关的知识,希望对你有一定的参考价值。

cf1555D. Say No to Palindromes

题意:

给出一个字符串,长度为n,而且都是a,b,c三个字符构成的,然后有m个询问
每个询问给出l r,问要想这个区间内任意长度字串都不是回文子串,至少要改多少个字符

题解:

我们思考一下,什么样的字符是符合要求的:

  1. 长度为2,相邻两个字符不能相同
  2. 长度为3,间隔为1的字符不能一样
  3. 长度为4,只要不满足长度为2和长度为3的情况,一定不是回文串

也就是只要不满足长度为2和长度为3的字符情况,就是符合要求的。要符合这两个要求,可以得知要求每三个字符都是不一样的,一共就三个字符,我们将三个字符全排列。
一个符合要求的字符串一定循环的,且循环节有6种情况:abc,acb,bac,bca,cab,cba
这样就可以实现三个位置都不相同
我们用前缀和预处理,用前缀和来实现

代码:

// Problem: D. Say No to Palindromes
// Contest: Codeforces - Educational Codeforces Round 112 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1555/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Data:2021-08-18 11:42:57
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 3e5 + 9;
string s1[10];
ll diff[10][maxn];
int main()
{
    //rd_test();
    ll n, m;
    read(n, m);
    string s;
    cin >> s;
    s= "#" + s; //为了下标从1开始
    s1[1]= "#";
    while (s1[1].size() <= n)
        s1[1]+= "abc";

    s1[2]= "#";
    while (s1[2].size() <= n)
        s1[2]+= "acb";

    s1[3]= "#";
    while (s1[3].size() <= n)
        s1[3]+= "bac";

    s1[4]= "#";
    while (s1[4].size() <= n)
        s1[4]+= "bca";

    s1[5]= "#";
    while (s1[5].size() <= n)
        s1[5]+= "cab";

    s1[6]= "#";
    while (s1[6].size() <= n)
        s1[6]+= "cba";
    //Time_test();
    for (int i= 1; i <= 6; i++) {
        diff[i][0]= 0;
        for (int j= 1; j <= n; j++) {
            if (s1[i][j] == s[j])
                diff[i][j]= diff[i][j - 1];
            else
                diff[i][j]= diff[i][j - 1] + 1;
        }
    }
    // cout << "--" << endl;
    while (m--) {
        int l, r;
        read(l, r);
        ll ans= INF_int;
        for (int i= 1; i <= 6; i++) {
            ans= min(ans, diff[i][r] - diff[i][l - 1]);
        }
        printf("%lld\\n", ans);
    }
    return 0;
}

以上是关于cf1555D. Say No to Palindromes的主要内容,如果未能解决你的问题,请参考以下文章

Educational 112 1555. D. Say No to Palindromes(前缀和)

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-题解

Say No to Palindromes

cf1555A. PizzaForces

cf1555B. Two Tables