CodeForces 1025C(思维题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40984919/article/details/81980332

作为一个小白,根本就想不到这道题怎么去做。只有求助各界大佬的帮忙(百度)

 

现在,把大概思想给大家讲一下。它这里主要运用到了要向其后边再重新加一个字符串

 

我们可以看一下上图红色代表第一次划分的位置 紫色为另外一次划分的位置

我们可以看到红色划分之后它变为了bafedc----如果将它构成一个环的话那么它和以前是没差的

紫色同理

所以我们可以把字符串写成为------abcdefabcdef组成一个环

这样我们便可以很好的去求解

代码:

           

#include <cstring>
#include <iostream>
#include <algorithm>
const int  MAX_N = 1e5 + 10;
using namespace std;

char s[MAX_N << 1];

int main(){
    scanf("%s",s+1);
    int n = strlen(s+1);
    for(int i = 1; i <= n; i++){
            s[i + n] = s[i];
    }
	int ans = 1;
	int j;
	for(int i = 1; i <= n * 2; i = j){
		for(j = i + 1; j <= n * 2; j++)
			if(s[j]==s[j-1])
				break;
		ans = max(ans, j - i);
	}
	ans = min(ans, n);
	printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_40984919/article/details/81980332