其他分享
首页 > 其他分享> > CF1555D Say No to Palindromes

CF1555D Say No to Palindromes

作者:互联网

暴力&构造 day2

题意:

字符集为{a,b,c}的长为\(n,(n\le 2e5)\)的字符串,每次修改可以把某个位置上的字母改成{a,b,c}中的任意一个。

\(m, (m \le 2e5)\)​​次询问,每次询问一个子串最少要进行多少次修改,使得这个字串中不包含长度大于等于2的回文串。

sol:

考虑构造长度任意的非回文字符串,将原串改成这个串,并统计每个位置上的修改次数,将区间询问转为求区间和。

注意到\(s_i \ne s_{i-1}\)且\(s_i \ne s_{i-2}\),那么一定有\(s_i=s_{i-3}\)。所以非回文的字符串一定是以形如abc的字符串作为循环节的。枚举每种循环节并生成字符串,并询问区间修改次数即可。

// Problem: D. Say No to Palindromes
// Contest: Codeforces - Educational Codeforces Round 112 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1555/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
#define ll long long
int n, m, k, tot;
struct _ {
	int l, r, ans;
}q[maxn];
int rd() {
	int s = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') {s = s * 10 + c - '0'; c = getchar();}
	return s * f;
}
char s[maxn];
char ch[3] = {'a','b','c'};
int a[3] = {0, 1, 2}, cnt[maxn];
int main() {
	n = rd(); m = rd();
	scanf("%s", s + 1);
	for (int i = 1; i <= m; i++) {
		q[i].l = rd(); q[i].r = rd();
		q[i].ans = maxn;
	}
	do {
		for (int i = 1; i <= n; i++) {
			if (s[i] != ch[a[i%3]]) cnt[i] = 1;
			else cnt[i] = 0;
		}
		for (int i = 1; i <= n; i++) cnt[i] += cnt[i-1];
		for (int i = 1; i <= m; i++) 
			q[i].ans = min(q[i].ans, cnt[q[i].r]-cnt[q[i].l-1]);
	}while (next_permutation(a,a+3));
	for (int i = 1; i <= m; i++) printf("%d\n", q[i].ans);
	return 0;
}

标签:Palindromes,CF1555D,int,询问,2e5,修改,Say,字符串,回文
来源: https://www.cnblogs.com/YjmStr/p/15250592.html