这道题目主要有两种做法,一种是对取余,对项链循环判断,另一种是把两个相同的项链连接起来,这样就由环变成了线性问题
我采用的是第一种方法,中间取余比较麻烦,还容易出错,如果这个项链是同一种颜色,容易死循环,需要手动break,建议使用第二种方法
/*
ID: zhuihun1
PROG: beads
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
int a[400];
int main()
{
ofstream fout ("beads.out");
ifstream fin ("beads.in");
int n,t,i,num=0,max=-1;
string str="";
fin>>n;
fin>>str;
char c;
bool f=true;
for(i=0;i<n&&f;i++)
{
num=0;
t=i;
while(str[t%n]=='w')
{
t++;num++;
if(num>n)
{
f=false;
break;
}
}
c=str[t];
while(str[t%n]==c||str[t%n]=='w')
{
if(num>n)
{
f=false;
break;
}
num++;t++;
}
t=(i+n-1)%n;
while(str[t%n]=='w')
{
if(num>n)
{
f=false;
break;
}
t=t+n-1;
num++;
}
c=str[t%n];
while(str[t%n]==c||str[t%n]=='w')
{
if(num>n)
{
f=false;
break;
}
num++;t=t+n-1;
}
if(num>max)
max=num;
}
if(max>n)
max=n;
fout<<max<<endl;
return 0;
}