USACO 1.1 Broken Necklace

/*
ID: aznfy1
PROG: beads
LANG: C++
*/

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

char necklace[500];int n,ans;

int left(int num)
{
    int ttl=0;int ad=num;
    char color=necklace[num];
    while((necklace[ad]==color||necklace[ad]=='w')&&ttl<n)
    {
        ad--;
        ttl++;
        if(color=='w')
        color=necklace[ad];
        if(ad<0)
        ad=n-1;
    }
    return ttl;
}

int right(int num)
{
   int ttl=0;int ad=num;
   char color=necklace[num];
   while((necklace[ad]==color||necklace[ad]=='w')&&ttl<n)
    {
        ad++;
        ttl++;
        if(color=='w')
        color=necklace[ad];
        if(ad>=n)
        ad=0;
    }
    return ttl;
}

int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    while(cin>>n)
    {
        ans=0;
        for(int i=0;i<n;i++)
        cin>>necklace[i];
        for(int i=0;i<n;i++)
        {
            if(left(i)+right(i+1)>ans)
            ans=left(i)+right(i+1);
        }
        if(ans>n)
        cout<<n<<endl;
        else
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/whatthefy/p/3082837.html