結果

問題 No.38 赤青白ブロック
ユーザー kou6839
提出日時 2014-12-04 15:13:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 368 ms / 5,000 ms
コード長 1,078 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 77,716 KB
実行使用メモリ 47,236 KB
最終ジャッジ日時 2024-12-23 12:40:20
合計ジャッジ時間 7,958 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.*;
import java.util.*;

public class Main {
	static boolean check(int ka,int kb,StringBuffer test){
		for(int i=0;i<test.length();i++){
			if(test.charAt(i)=='R'){
				if(i+ka<test.length() && test.charAt(i+ka)=='R') return false;
			}else if(test.charAt(i)=='B'){
				if(i+kb<test.length() && test.charAt(i+kb)=='B')return false;
			}
		}
		return true;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int ka = sc.nextInt();
		int kb = sc.nextInt();
		String ss=sc.next();
		int[] block = new int[20];
		int temp=0;
		for(int i=29;i>=0;i--){
			if(ss.charAt(i)=='R' || ss.charAt(i)=='B' ){
				block[temp]=i;
				temp++;
			}
		}
		int ans=Integer.MAX_VALUE;
		for(int i=0;i< (1<<21) ;i++){
			if(Integer.bitCount(i)>=ans) continue;
			StringBuffer test = new StringBuffer(ss);
			for(int j=0;j<20;j++){
				if( ( (i>>j) &1) ==1 ){
					test.deleteCharAt(block[j]);
				}
			}
			if(check(ka,kb,test)){
				ans=Integer.bitCount(i);
			}
		}
		System.out.println(30-ans);
	}
}			
0