結果

問題 No.154 市バス
ユーザー jp_stejp_ste
提出日時 2015-03-18 06:32:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 1,607 bytes
コンパイル時間 2,548 ms
コンパイル使用メモリ 77,556 KB
実行使用メモリ 47,748 KB
最終ジャッジ日時 2024-04-21 08:24:52
合計ジャッジ時間 6,067 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 464 ms
47,684 KB
testcase_01 AC 383 ms
46,988 KB
testcase_02 AC 486 ms
47,748 KB
testcase_03 AC 404 ms
47,576 KB
testcase_04 AC 490 ms
47,716 KB
testcase_05 AC 113 ms
41,176 KB
testcase_06 AC 116 ms
41,264 KB
testcase_07 AC 508 ms
47,708 KB
testcase_08 AC 121 ms
41,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		for(int i=0; i<T; i++) {
		
			char[] S = sc.next().toCharArray();
						
			//一番右はR
			if(S[S.length-1] != 'R') {
				System.out.println("impossible");
				continue;
			}
			
			//RとGの個数は同じ
			int rNum = getCharCount(S, 'R');
			int gNum = getCharCount(S, 'G');
			if(rNum>0 && gNum>0 && (rNum==gNum)) {
				
			} else {
				System.out.println("impossible");
				continue;
			}
			
			int r, g, w;
			//一番右のGより右にWがあってはだめ
			g = getCharIndex(S, S.length, 'G');
			w = getCharIndex(S, S.length, 'W');
			if(g>0 && w>0 && (w<g)) {
				
			} else {
				System.out.println("impossible");
				continue;
			}
			
			for(int j=0; j<rNum; j++) {
			
				r = getCharIndex(S, S.length, 'R');
				g = getCharIndex(S, r,        'G');
				w = getCharIndex(S, g,        'W');
								
				if(r<0 || g<0 || w<0) {
					break;
				}
				
				if( (r > g && r > w) && (w < g && w < r) ) {
					S[r] = S[g] = S[w] = ' ';
				} else {
					break;
				}
			}
			
			rNum = getCharCount(S, 'R');
			if(rNum == 0) {
				System.out.println("possible");
			} else {
				System.out.println("impossible");
			}
		}
	}
	
	static int getCharIndex(char[] list, int edge, char c) {
		for(int i=edge-1; i>=0; i--) {			
			if(list[i] == c) return i;
		}
		return -1;
	}
	
	static int getCharCount(char[] list, char c) {
		int ret = 0;
		for(int i=0; i<list.length; i++) {
			if(list[i] == c) ret++;
		}
		return ret;
	}
}
0