結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 150 ms
41,352 KB
testcase_06 AC 151 ms
41,060 KB
testcase_07 AC 667 ms
47,360 KB
testcase_08 AC 150 ms
41,168 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;
			}
			
			for(int j=0; j<rNum; j++) {
			
				int r = getCharIndex(S, S.length, 'R');
				int g = getCharIndex(S, r,        'G');
				int 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(c == 'G') {
				if(list[i] == 'W') return -1;
			}
			
			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