/*
ID: lucien23
PROG: beads
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;
int main()
{
ifstream infile("beads.in");
ofstream outfile("beads.out");
if(!infile || !outfile)
{
cout<<"file operation failure!"<<endl;
return -1;
}
int N;
string nacklace;
infile>>N>>nacklace;
int maxNum=0;
int i=0,right,left;
char lPivot,rPivot;
while(i<N)
{
rPivot=nacklace[i];
right=i+1;
while(right<2*N && right%N!=i)
{
if(rPivot=='w')
rPivot=nacklace[right%N];
if(nacklace[right%N]!='w' && nacklace[right%N]!=rPivot)
break;
right++;
}
//得先判断i-1是否有效
if(right-i==N)
{
maxNum=N;
break;
}
left=i-2+N;
lPivot=nacklace[(left+1)%N];
while (left>=0 && !(left>=i && left<right))
{
if(lPivot=='w')
lPivot=nacklace[left%N];
if(nacklace[left%N]!='w' && nacklace[left%N]!=lPivot)
break;
left--;
}
int num=right-left+N-1;
if(num>maxNum)
{
maxNum=num;
}
cout<<"i="<<i<<" left="<<left<<" right="<<right<<" num="<<num<<endl;
i=right;
}
outfile<<maxNum<<endl;
return 0;
}
USACO Section 1.1 Broken Necklace
最新推荐文章于 2020-09-22 21:15:15 发布