[Java] Broken Necklace

/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: lincans1
LANG: JAVA
TASK: beads 
*/
import java.io.*;
import java.util.*;

class beads {
    
    
	public beads() throws IOException {
    
    
		// Use BufferedReader rather than RandomAccessFile; it's much faster
		BufferedReader f = new BufferedReader(new FileReader("beads.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
		int N = Integer.parseInt(f.readLine());
		String str = f.readLine();
		String string = str + str;
		int maxCount = 0;
		Character flag = null;
		for (int i = 0; i< string.length(); i++) {
    
    
			int count = 0;
			flag = null;
			for (int j = i, n = Math.max(0, i + 1- str.length()); j >= n; j--) {
    
    
				char ch = string.charAt(j);
				if (ch == 'w') {
    
    
				}
				else if (flag == null) {
    
    
					flag = ch;
				}
				else if (flag != ch) {
    
    
					break;
				}
				count++;
			}
			flag = null;
			for (int j = i + 1, n = Math.min(string.length(), i + 1 + str.length() - count); j < n; j++) {
    
    
				char ch = string.charAt(j);
				if (ch == 'w') {
    
    
				}
				else if (flag == null) {
    
    
					flag = ch;
				}
				else if (flag != ch) {
    
    
					break;
				}   		
				count++;
			}
			maxCount = Math.max(maxCount, count);
		}
		out.println(maxCount);
		out.close();
	}
	public static void main (String [] args) throws IOException {
    
    
		new beads();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41714373/article/details/111875606