Sat Jun 30, 2007 10:51 pm
/*
PROG: beads
*/
/* Recursive solution which exploits the
pattern in the problem statement. Be careful
with the white beads. */
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <math.h>
using namespace std;
int bcount(string neck,int st)
{
int cnt=1;
int cur=st; int oldcur=st;
int cta=0;
do {
oldcur=cur;
cur=(cur+1)%neck.size();
if (cur==st) break;
if (neck[cur]=='w') neck[cur]=neck[oldcur];
if (!(neck[oldcur]=='w') && (neck[cur]!=neck[oldcur])) break;
cnt++;
cta++;
} while (1);
int ct=0;
for (int k=st; ct<=cta; ct++, k=(k+1)%neck.size())
neck[k]='.';
cur=(st-1+neck.size())%neck.size(); oldcur=cur;
if (neck[cur]!='.') cnt++;
do {
oldcur=cur;
cur=(cur+neck.size()-1)%neck.size();
if (neck[cur]=='w') neck[cur]=neck[oldcur];
if (((neck[oldcur]!='w') && (neck[cur]!=neck[oldcur])) || (neck[cur]=='.')) break;
cnt++;
} while (cur!=st);
return cnt;
}
void main()
{
fstream fin,fout;
int i;
fin.open("beads.in",ios::in);
cin=fin;
fout.open("beads.out",ios::out);
int no;
string nck;
cin >> no >> nck;
int max=0;
for (int k=0; k<no; k++)
if (bcount(nck,k)>max) max=bcount(nck,k);
fout << max << endl;
fout.close();
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.