题解 CF1025C【Plasticine zebra】

思维题。

为了叙述方便,我们记字符串 \(S\) 翻转后的结果为 \(\widehat{S}\)

假设我们选择翻转的两段子串为 \(A,B\)(即 \(S=AB\)),那么得到的新字符串为 \(S^\prime=\widehat{A}\widehat{B}\)

我们把字符串再次翻转,得到 \(\widehat{S^\prime}=BA\),显然这次操作不会影响结果,即最长的黑白相间子串。

至此,问题转化为:对字符串 \(S\) 进行循环移位(位数随意),最大化最长黑白相间子串长度。直接断环成链统计即可。

// Problem: CF1025C Plasticine zebra
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1025C
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
const int N = 2e5+5;

int n;
char s[N];
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

int main() {
	scanf("%s", s);
	n = strlen(s);
	rep(i, n, n+n-1) s[i] = s[i-n];
	int now = 1, ans = 0;
	rep(i, 1, n+n-1) {
		if(s[i] == s[i-1]) now = 1;
		else ++now;
		chkmax(ans, now);
	}
	chkmin(ans, n);
	printf("%d\n", ans);
	return 0;
}
posted @ 2022-07-31 19:45  rui_er  阅读(46)  评论(0)    收藏  举报