【枚举】Broken Necklace

首先一定要先把题意读懂,读题时一定要专注仔细。题目读懂后还是思考,思考好了,条理清晰了,才可以开始码代码。

因为是环所以mencpy(s + n, s, n);然后, 用a表示前一段的连续长度, b表示当前段的连续长度, w表示在当前出现的w的连续数目.

然后还有一个需要注意的就是,如果全是一样的bead,为了防止结果超过n,我们直接min(n, res);就好了。

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
char s[700 + 24];
char c = '0';
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int n, m, res = 0, a = 0, b = 0, w = 0;	
	scanf("%d", &n);
	m = n << 1;
	getchar();
	scanf("%s", s);
	memcpy(s + n, s, n);
	for(int i = 0; i < m; i++) {
		if(s[i] == 'w')	{b++; w++;}
		else if(s[i] == c)	{b++; w = 0;}
		else	{
			res = max(res, a + b); 
			a = b - w; b = w + 1; 
			w = 0; c = s[i];
		}
	}
	res = max(res, a + b);
	printf("%d\n", min(res, n));
	return 0;
}
/**/

猜你喜欢

转载自blog.csdn.net/wen__han/article/details/84669477