All pastes #216868 Raw Edit

Indy

public c v1 · immutable
#216868 ·published 2006-10-23 10:13 UTC
rendered paste body
/*ID: indy901PROG: beadsLANG: C*/#include <stdio.h>#include <stdlib.h>int N;int i;char* beads;char* newBeads;int breakNecklace() {	int startBeads;	int endBeads;	int j;	int k;	int gotColor;	char color;		newBeads = ( char* )malloc( sizeof( char ) * N );	k = 0;	for ( j = i; j < N; j++ ) {		newBeads[ k ] = beads[ j ];		k++;	}	for ( j = i - 1; j >= 0; j-- ) {		newBeads[ k ] = beads[ j ];		k++;	}	startBeads = 0;	gotColor = 0;	for ( j = 0; j < N; j++ ) {		if ( newBeads[ j ] == 'w' )			startBeads++;		else if ( gotColor ) {			if ( newBeads[ j ] == color )				startBeads++;			else {				j--;				break;			}		}		else {			color = newBeads[ j ];			gotColor = 1;			startBeads++;		}	}	endBeads = 0;	gotColor = 0;	for ( k = N - 1; k > j; k-- ) {		if ( newBeads[ k ] == 'w' )			endBeads++;		else if ( gotColor ) {			if ( newBeads[ k ] == color )				endBeads++;			else				break;		}		else {			color = newBeads[ k ];			gotColor = 1;			endBeads++;		}	}	return startBeads + endBeads;}int main() {	int candidateBeads;	int maxBeads;	FILE* fp;		fp = fopen( "beads.in" , "r" );	fscanf( fp , "%d" , &N );	beads = ( char* )malloc( sizeof( char ) * ( N + 1 ) );	fscanf( fp , "%s" , beads );	fclose( fp );	maxBeads = 0;	for ( i = 0; i < N; i++ ) {		candidateBeads = breakNecklace();		if ( candidateBeads > maxBeads )			maxBeads = candidateBeads;	}	fp = fopen( "beads.out" , "w" );	fprintf( fp , "%d\n" , maxBeads );	fclose( fp );	free( beads );	free( newBeads );	return 0;}