All pastes #741713 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-18 22:30 UTC
rendered paste body
/*
ID: jennife2
LANG: JAVA
TASK: beads
*/

import java.io.*;

class beads{

	private static int counter = 0;
	
	public static void main(String[] args) throws IOException{
		BufferedReader console = new BufferedReader(new FileReader("beads.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
		
		int places = Integer.parseInt(console.readLine());
		int[] necklace = new int[places];
		char[] coloring = new char[places];
		coloring[0] = (char) console.read();
		necklace[0] = 1;
		while(console.ready()){
			char cur = (char) console.read();
			System.out.print(cur);
			if (cur != coloring[counter]){
				counter++;
				coloring[counter] = cur;
			}
			necklace[counter]++;
		}
		
		// -------- End Input
		
		if(counter>0&&coloring[counter]==coloring[0]){
			necklace[0] += necklace[counter];
			counter--;
		}
		counter++;

		boolean flag = true;
		while(flag){
			flag = false;
			for(int i=0;i<counter;i++){
				if(coloring[i]=='w'){
					if(coloring[circ(i-1)]==coloring[circ(i+1)]&&coloring[circ(i-1)]!='x'){
						necklace[circ(i-1)] += necklace[i] + necklace[circ(i+1)];
						coloring[i] = 'x';
						coloring[circ(i+1)] = 'x';
						flag = true;
					}
				}
			}
			int[] newneck = new int[counter];
			char[] newcol = new char[counter];
			int newcount=0;
			for(int i=0;i<counter;i++){
				if(coloring[i]!='x'){
					newneck[newcount] = necklace[i];
					newcol[newcount] = coloring[i];
					newcount++;
				}
			}
			counter = newcount;
			necklace=newneck;
			coloring=newcol;
		}
		// -------- End Modify Input
		
		// -------- Time to Solve!
		int lg = necklace[0];
		for(int i=0; i<counter;i++){
			if(coloring[i]!='w'){
				int cur = necklace[i];
				int next = 1;
				if (coloring[circ(i-1)]=='w') cur += necklace[circ(i-1)];
				if (coloring[circ(i+1)]=='w'){ cur += necklace[circ(i+1)];next++;}
				cur += necklace[circ(next+i)];
				if (coloring[circ(i+next+1)]=='w') cur += necklace[circ(i+next+1)];
				if(cur>lg)lg=cur;
			}
		}
		System.out.print(lg);
		out.print(lg+"\n");
		out.close();
		System.exit(0);
	}
	
	public static int circ(int bleh){
		return (bleh+counter)%counter;
	}
}