All pastes #741812 Raw Edit

Unnamed

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

import java.io.*;

class beads{

	
	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 counter = 0;
		int places = Integer.parseInt(console.readLine());
		int[] necklace = new int[places+1];
		char[] coloring = new char[places+1];
		coloring[0] = (char) console.read();
		System.out.println(places);
		System.out.print(coloring[0]);
		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]++;
		}
		//counter++;
		// -------- End Input
		if(counter>0&&coloring[counter]==coloring[0]){
			necklace[0] += necklace[counter];
			counter--;
		}
		boolean flag = true;
			while(flag){
				flag = false;
				if(counter>1){
					for(int i=0;i<counter;i++){
						if(coloring[i]=='w'){
							if(coloring[circ(counter, i-1)]==coloring[circ(counter, i+1)]&&coloring[circ(counter, i-1)]!='x'){
								necklace[circ(counter, i-1)] += necklace[i] + necklace[circ(counter, i+1)];
								coloring[i] = 'x';
								coloring[circ(counter, i+1)] = 'x';
								flag = true;
							}
						}
					}
					int[] newneck = new int[counter+1];
					char[] newcol = new char[counter+1];
					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;
			}
		}
		if(counter>1&&coloring[counter-1]==coloring[0]){
			necklace[0] += necklace[counter-1];
			counter--;
		}
		// -------- End Modify Input
		// -------- Time to Solve!
		int lg = necklace[0];
		if (counter>1){
		for(int i=0; i<counter;i++){
			if(coloring[i]!='w'){
				int cur = necklace[i];
				int next = 1;
				if (coloring[circ(counter, i-1)]=='w') cur += necklace[circ(counter, i-1)];
				if (coloring[circ(counter, i+1)]=='w'){ cur += necklace[circ(counter, i+1)];next++;}
				cur += necklace[circ(counter, next+i)];
				if (coloring[circ(counter, i+next+1)]=='w') cur += necklace[circ(counter, i+next+1)];
				if(cur>lg)lg=cur;
			}
		}}

		System.out.println();
		for(int i =0;i<counter;i++){
			System.out.print(coloring[i]+""+necklace[i]+" ");
	
		}
		System.out.println();
		System.out.print(counter);
		System.out.println();
		System.out.println(lg);
		out.print(lg+"\n");
		out.close();
		System.exit(0);
	}
	
	public static int circ(int counter, int bleh){
		return (bleh+counter)%counter;
	}
}