結果

問題 No.154 市バス
ユーザー htensaihtensai
提出日時 2020-02-17 17:31:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,290 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 76,968 KB
実行使用メモリ 43,804 KB
最終ジャッジ日時 2024-04-16 03:52:48
合計ジャッジ時間 4,625 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 191 ms
42,980 KB
testcase_01 AC 197 ms
43,180 KB
testcase_02 AC 197 ms
43,152 KB
testcase_03 AC 198 ms
43,804 KB
testcase_04 AC 196 ms
43,208 KB
testcase_05 AC 52 ms
36,848 KB
testcase_06 AC 52 ms
36,816 KB
testcase_07 AC 172 ms
43,172 KB
testcase_08 AC 53 ms
36,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    int n = Integer.parseInt(br.readLine());
	    StringBuilder sb = new StringBuilder();
	    for (int i = 0; i < n; i++) {
	        char[] arr = br.readLine().toCharArray();
	        int r = 0;
	        int g = 0;
	        int w = 0;
	        boolean type = true;
	        for (int j = arr.length - 1; j >= 0; j--) {
	            if (arr[j] == 'W') {
	                if (g > 0) {
	                    g--;
	                    w++;
	                } else if (g == 0 && w == 0) {
	                    type = false;
	                    break;
	                }
	            } else if (arr[j] == 'G') {
	                if (r == 0) {
	                    type = false;
	                    break;
	                }
	                r--;
	                g++;
	            } else {
	                r++;
	            }
	        }
	        if (r > 0 || g > 0) {
	            type = false;
	        }
	        if (type) {
	            sb.append("possible");
	        } else {
	            sb.append("impossible");
	        }
	        sb.append("\n");
	    }
		System.out.print(sb);
    }
}
0