USACO试题beads的两种解法

package Karl.Water;

/*
LANG: JAVA
TASK: beads
*/


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.LinkedList;

public class beads {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			BufferedReader br = new BufferedReader(new FileReader("beads.in"));
			PrintWriter pw = new PrintWriter (new FileWriter("beads.out"));
			
			int length = Integer.parseInt(br.readLine());
			String temp = br.readLine();
			LinkedList necklace = new LinkedList();
			for (int i=0;i=max){
					max = result;
				}
			}
			pw.println(max);
			pw.close();
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static int find(LinkedList necklace,int startPoint ,int endPoint,int direction){
		if (direction ==0 ){
			int i=startPoint+1;
			for (;i<=endPoint-1;i++){
				if (necklace.get(i)!=necklace.get(startPoint)&&necklace.get(i)!='w'){
					break;
				}
			}
			return i-1;
		} else {
			int i=startPoint-1;
			for (;i>=endPoint+1;i--){
				if (necklace.get(i)!=necklace.get(startPoint)&&necklace.get(i)!='w'){
					break;
				}
			}
			return i+1;
		}
	}

}
package Karl.Water;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class beads1 {
	List list = new ArrayList();
	public int judge(String s){
		if(s.charAt(0)=='w'){
			return judgeE(s);
		}else{
			return judgeB(s);
		}
	}
	public int judgeE(String s){
		int result=0;
		int j=0;
		char c=s.charAt(s.length()-1);
		for(int k=s.length()-1;k>0;k--){
			j++;
			if(c!=s.charAt(k-1)&&s.charAt(k-1)!='w'){
				list.add(j);
				System.out.println(j);
				j=0;
				c=s.charAt(k-1);
			}
		}
		int h = 0;
		for(int k=0;k(list.get(p)+list.get(0))?result:(list.get(p)+list.get(0));
			}else{
				result = result>(list.get(p)+list.get(p+1))?result:(list.get(p)+list.get(p+1));
			}
		}
		return result;
	}
	public int judgeB(String s){
		int result=0,f=0;
		int j=0;
		char c=s.charAt(0);
		for(int k=0;k(list.get(p)+list.get(0))?result:(list.get(p)+list.get(0));
			}else{
				result = result>(list.get(p)+list.get(p+1))?result:(list.get(p)+list.get(p+1));
			}
		}
		return result;
	}
	public static void main(String[] args) throws IOException {
		beads1 b = new beads1();
		BufferedReader br = new BufferedReader(new FileReader("beads.in"));
		PrintWriter pw = new PrintWriter (new FileWriter("beads.out"));
		String s1 = br.readLine();
		String s2 = br.readLine();
		pw.println(b.judge(s2+s2));
		pw.close();
		br.close();
	}
}


你可能感兴趣的:(Java程序)