ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF1555D Say No to Palindromes

2021-09-10 11:35:36  阅读:141  来源: 互联网

标签:Palindromes CF1555D int 询问 2e5 修改 Say 字符串 回文


暴力&构造 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有