洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace
坏掉的项链Broken Necklace
难度:★
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
//Mystery_Sky
//
#define M 1000
string s;
int n, ans, flag, maxx, sum;
int l[M], r[M];
bool it[M];
int main() {
scanf("%d", &n);
cin >> s;
for(int i = 0; i < n; i++) {
sum = 2;
int iter_1, iter_2;
char ster_1, ster_2;
iter_1 = i-1, iter_2 = i+2;
if(iter_1 < 0) iter_1 = n;
if(iter_2 >= n) iter_2 -= n;
ster_1 = s[i], ster_2 = s[i+1];
while(!ans) {
// printf("iter_1=%d\n", iter_1);
if(s[iter_1] == s[iter_1+1]) sum++;
else if(s[iter_1] == 'w') sum++;
else if(s[iter_1] == ster_1) sum++;
else if(ster_1 == 'w') {
sum++;
ster_1 = s[iter_1];
}
else break;
iter_1--;
if(iter_1 < 0) iter_1 = n-1;
if(iter_1 == iter_2-1) ans = n;
}
while(!ans) {
if(s[iter_2] == s[iter_2-1]) sum++;
else if(s[iter_2] == 'w') sum++;
else if(s[iter_2] == ster_2) sum++;
else if(ster_2 == 'w') {
sum++;
ster_2 = s[iter_2];
}
else break;
iter_2++;
if(iter_2 >= n) iter_2 = 0;
}
if(ans) break;
maxx = max(sum, maxx);
}
if(ans == n) printf("%d\n", ans);
else if(maxx > n) printf("%d\n", n);
else printf("%d\n", maxx);
return 0;
}
唯愿,青春不辜负梦想,未来星辰闪耀