记录编号 169356 评测结果 AAAAAAAAA
题目名称 破碎的项链 最终得分 100
用户昵称 GravatarNVIDIA 是否通过 通过
代码语言 C++ 运行时间 0.003 s
提交时间 2015-07-08 17:12:40 内存使用 0.29 MiB
显示代码纯文本
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
int b1[998],r1[998],b2[998],r2[998];
int n;char ch[998];
int M(int a,int b)
{
    if (a>b) return a;
    else return b;
}
int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    scanf("%d",&n);
    scanf("%s",ch);
    for (int i=0;i<n;i++) ch[i+n]=ch[i];
	//printf("%s\n",ch);
    b1[0]=r1[0]=0;
    for (int i=1;i<=n;i++)
        if (ch[i]=='b')
        {
            b1[i]=b1[i-1]+1;r1[i]=0;
        }
        else if (ch[i]=='r')
        {
            b1[i]=0;r1[i]=r1[i-1]+1;
        }
        else 
        {
            b1[i]=b1[i-1]+1;r1[i]=r1[i-1]+1;
        }
    b2[2*n+1]=r2[2*n+1]=0;
    for (int i=2*n;i>=1;i--)
        if (ch[i]=='b')
        {
            b2[i]=b2[i+1]+1;r2[i]=0;
        }
        else if (ch[i]=='r')
        {
            b2[i]=0;r2[i]=r2[i+1]+1;
        }
        else 
        {
            b2[i]=b2[i+1]+1;r2[i]=r2[i+1]+1;
        }
    int ans=0;
    for (int i=1;i<=2*n;i++)
        ans=M(M(b1[i],r1[i])+M(b2[i+1],r2[i+1]),ans);
    if (ans>n) ans=n;
    printf("%d\n",ans);
    return 0;
}