結果

問題 No.154 市バス
ユーザー GrenacheGrenache
提出日時 2016-02-14 10:26:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 390 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 3,182 ms
コンパイル使用メモリ 77,812 KB
実行使用メモリ 48,128 KB
最終ジャッジ日時 2024-04-21 09:08:49
合計ジャッジ時間 6,713 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 385 ms
47,048 KB
testcase_01 AC 386 ms
48,000 KB
testcase_02 AC 342 ms
46,736 KB
testcase_03 AC 384 ms
48,128 KB
testcase_04 AC 390 ms
46,516 KB
testcase_05 AC 120 ms
41,388 KB
testcase_06 AC 106 ms
39,888 KB
testcase_07 AC 323 ms
46,096 KB
testcase_08 AC 122 ms
41,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main_yukicoder154 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        for (int tcase = 0; tcase < t; tcase++) {
        	char[] s = sc.next().toCharArray();
        	int n = s.length;

        	boolean flag = true;
        	int r = 0;
        	int g = 0;
        	int w = 0;
//        	int dr = 0;
//        	int dg = 0;
//        	int dw = 0;
        	for (int i = n - 1; flag && i >= 0; i--) {
        		char c = s[i];

        		if (c == 'R') {
        			r++;
//        			dr++;
        		} else if (c == 'G') {
        			g++;
//        			dg++;
        		} else {
        			w++;
//        			dw++;
        		}

        		if (g > r) {
        			flag = false;
        		}
        		if (w > 0 && g == 0) {
        			flag = false;
        		}
        		if (w > 0 && g > 1) {
        			w = 0;
        			g--;
        			r--;
        		}
        	}

        	if (r != 1 || g != 1 || w == 0) {
        		flag = false;
        	}

//        	System.out.printf("%d %d %d\n", dr, dg, dw);

        	if (flag) {
        		System.out.println("possible");
        	} else {
        		System.out.println("impossible");
        	}
        }

        sc.close();
    }
}
0