Broken Necklace

/*
ID: m1871091
LANG: C++11                 
TASK: beads
*/
#define _CRT_SECURE_NO_WARNINGS
#define local

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char a[400];

int main(){
#ifdef local 
		freopen("beads.in", "r", stdin);
		freopen("beads.out", "w", stdout);
#endif // local
		int n;
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}
		int total = 0,maxn=-1;
		for (int i = 0;i<n; i++){
			char ch = ' ';
			int flag = 0;
			int j = i;
			for (j;; j++){
				if (a[j%n] == 'w' && (j%n) != i){
					total++;
				}
				else if(!flag){
					ch = a[j%n];
					total++;
					flag = 1;
				}
				else if (a[j % n] == ch && (j%n) != i){
					total++;
				}
				else break;
			}
			if ((j%n) == i){ maxn = max(maxn, total); break; }
			char ch1 = 'o'; flag = 0;
			for (int j1 = i-1;(j1+n)%n!=j; j1--){
				if (a[(j1+n) % n] == 'w'){
					total++;
				}
				else if (!flag){
					ch1 = a[(j1 + n) % n];
					total++;
					flag = 1;
				}
				else if (a[(j1 + n) % n] == ch1){
					total++;
				}
				else break;
			}
			maxn = max(maxn, total);
			total = 0;
		}
		cout << maxn << endl;
	return 0;
}

你可能感兴趣的:(C++,USACO,ACM入门)