usaco:Broken Necklace

没什么好说

/*
ID: Jang Lawrence
PROG: beads
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int Max(int x,int y){if(x<y) return y;return x;}
int n;
char  a[1111];
int ans;
int f(char x,int id,int k)
{
    int nu=0;
    for(int i=id;nu<n;i=(i+k)%n)
    {
        if(a[i]==x||a[i]=='w') nu++;
        else break;
    }
    return nu;
}
int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
   scanf("%d",&n);
   scanf("%s",a);
    ans=2;
    for(int i=0;i<n;++i)
    {
        int j=(i+1)%n;
        int ki=0,kj=0;
      ki=Max(f('b',i,-1),f('r',i,-1));
      kj=Max(f('b',j,1),f('r',j,1));
      ans=Max(ans,ki+kj);
    }
    printf("%d\n",ans>n?n:ans);
    return 0;
}


你可能感兴趣的:(usaco:Broken Necklace)