Usaco1.1 beads

/*
ID: ljfcnya1
PROG: beads
LANG: C++
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int maxn = 10000;

int n, m;

int ans[maxn];

string s;

int main() {
    freopen("beads.in", "r", stdin);
    freopen("beads.out", "w", stdout);
    scanf("%d", &n);
    getchar();
    cin>>s;
    s += s;
    REP(i, 0, n) {
        int j = i - 1;
        while(s[++j] == 'w');
        char tmp = s[j];
        while((s[++j] == tmp || s[j] == 'w') && (j >= i && j < i + n));
        ans[i] += j - i;
        if(ans[i] == n)
            continue;   
        j = i + n;
        while(s[--j] == 'w');
        tmp = s[j];
        while((s[--j] == tmp || s[j] == 'w') && (j >= i && j < i + n));
        ans[i] += i + n - j - 1;
        if(ans[i] > n)  ans[i] = n;
    }
    int sum = 0;
    REP(i, 1, n)
        sum = max(sum, ans[i]);
    printf("%d\n", sum);
    return 0;
}

你可能感兴趣的:(Usaco1.1 beads)