本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@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 , 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 letters of the Latin alphabet (in lowercase).
You are given a string of length , each character of the string is one of the first letters of the Latin alphabet (in lowercase).
You have to answer queries — calculate the cost of the substring of the string from -th to -th position, inclusive.
Input
The first line contains two integers and () — the length of the string and the number of queries.
The second line contains the string , it consists of characters, each character one of the first Latin letters.
The following lines contain two integers and () — parameters of the -th query.
Output
For each query, print a single integer — the cost of the substring of the string from -th to -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 , which can be changed to in one operation;
-
in the second query, the substring is , which can be changed to in two operations;
-
in the third query, the substring is , which can be left unchanged;
-
in the fourth query, the substring is , which can be changed to in one operation.
题目大意
给你一个长度为的字符串和次询问,每次询问到的子串至少修改几个元素使得这个子串中的所有长度大于1的子串都不是回文串。
解题思路
这道题字符串中只能出现种字母。要想所有子串中没有回文串,首先相邻两个不能相同,因为就是回文串。
所以不妨假设的左边是。那么的右边必须是。否则、都包含回文串。
也就是说,旁边必须有和,即任意连续个元素必须取遍
所以最终不含回文串的长串长这样:abcabcabc...
这样的串有几种呢?种。所以要修改的最终目标只有种合法情况。对于每种合法情况,我们可以预知哪一位需要更改。像上题一样使用前缀数组即可快速得到到的元素一共需要修改几个。
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…