All pastes #744694 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-21 18:35 UTC
rendered paste body
/*
ID: jennife2
LANG: JAVA
TASK: calfflac
*/

import java.io.*;

public class calfflac{

	public static int ops = 0;
	public static void main(String[] args) throws IOException{
		
		BufferedReader console = new BufferedReader(new FileReader("calfflac.in"));
		BufferedWriter out = new BufferedWriter(new PrintWriter(new FileWriter("calfflac.out")));
		String s = ""; // string to be handled
		String read = "";
		while(console.ready()){
			ops++;
			String newline = console.readLine();
			read+="\n"+newline;
			for(int i=0;i<newline.length();i++){
				ops++;
				if ((int)newline.charAt(i)>96&&(int)newline.charAt(i)<123||(int)newline.charAt(i)>64&&(int)newline.charAt(i)<91){
					s += newline.charAt(i);
				}
			}
		}
		s = s.toUpperCase();
		String read2 = read.toUpperCase(); // input with all uppercase

		String pall = "";
		int longest = 0;
		int posLong = 0; // start position of longest palindrome
		
		// odd length palindromes
		for(int i=0; i<s.length();i++){
			int z = 1;
			while(i-z>=0&&i+z<s.length()&&s.charAt(i-z)==s.charAt(i+z)){
				z++;
				ops++;
			}
			z--;
			if (z*2+1>longest){
				longest = z*2+1;
				posLong = i-z;
				pall = s.substring(i-z,i+z+1);
			}
		}
		// even length palindromes
		for(int i=0; i<s.length();i++){
			int z = 1;
			while(i-z+1>=0&&i+z<s.length()&&s.charAt(i-z+1)==s.charAt(i+z)){
				z++;
				ops++;
			}
			z--;
			if (z*2>longest||z*2==longest&&i-z+1<posLong){
				longest = z*2;
				posLong = i-z+1;
				pall = s.substring(i-z+1,i+z+1);
			}
		}
		
		int curChar = 0; // current character position
		int start = 0;
		int end = 0;
		if (longest>0){
			for(int i=0;curChar>-1&&i<read2.length();i++){
				ops++;
				if(read2.charAt(i)==pall.charAt(curChar)){
					if(curChar == 0) start = i;
					curChar++;
					if(curChar == pall.length()){
						end = i+1;
						curChar = -1;
					}
				}
				else if(curChar > 0){
					if ((int)read2.charAt(i)>64&&(int)read2.charAt(i)<91){
						curChar = 0;
						i--;
					}
				}
			}
			System.out.println(ops);
			//System.out.println(start+","+end);
			//System.out.println(pall);
			System.out.print(longest+"\n"+read.substring(start,end)+"\n");
			out.write(longest+"\n"+read.substring(start,end)+"\n");
		}
		else{
			out.write("0\n\n");
		}
		out.close();
		System.exit(0);
	}
}