USACO section1.1 Broken Necklace

时间:2024-07-18 23:04:02
 /*
ID: vincent63
LANG: C
TASK: beads
*/
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int findmax(char s[],int n){
int i,j,count,max,l,r;
char left,right;
max=;
count=;
int p;
for(i=,j=;j<n;i++,j++){ l=i;
r=j;
while(s[l]=='w'){
l--;
count++;
}
while(s[r]=='w'){
r++;
count++;
} left=s[l];
right=s[r]; for(;l>=;l--){
if(s[l]==left||s[l]=='w'){
count++;
}else{
break;
}
}
for(;r<n;r++){
if(s[r]==right||s[r]=='w'){
count++;
}else{
break;
}
}
if(count>max){
max=count;
p=i;
//printf("%dth count:%d\n",p,count);
}
count=;
}
if(max>n/){
return n/;
}else{
return max;
} }
int main () {
FILE *fin = fopen ("beads.in", "r");
FILE *fout = fopen ("beads.out", "w"); int n;
char buf[];
char res[];
fgets(buf,,fin);
n=atoi(buf); char necklace[*n+];
char buf2[n+]; fgets(necklace,n+,fin); strcpy(buf2,necklace); strcat(necklace,buf2);
int i;
/*
for(i=0;i<2*n;i++){
printf("%c",necklace[i]);
}
printf("\n");
*/
int max;
max=findmax(necklace,*n); //itoa(max,res,10);
sprintf(res,"%d\n",max);
fputs(res,fout);
//printf("%d\n",max); return ;
}