結果

問題 No.154 市バス
ユーザー takeya_okinotakeya_okino
提出日時 2019-08-02 23:00:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,270 bytes
コンパイル時間 3,712 ms
コンパイル使用メモリ 73,544 KB
実行使用メモリ 60,664 KB
最終ジャッジ日時 2023-09-18 18:30:49
合計ジャッジ時間 6,210 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 406 ms
60,264 KB
testcase_01 AC 407 ms
60,408 KB
testcase_02 AC 400 ms
60,080 KB
testcase_03 AC 427 ms
60,664 KB
testcase_04 AC 411 ms
60,052 KB
testcase_05 AC 126 ms
55,464 KB
testcase_06 AC 126 ms
55,480 KB
testcase_07 AC 397 ms
60,020 KB
testcase_08 AC 128 ms
55,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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++) {
      String s = sc.next();
      int len = s.length();
      int[] w = new int[len];
      int[] g = new int[len];
      int[] r = new int[len];
      int w1 = 0;
      int g1 = 0;
      int p = 0;
      for(int j = 0; j < len; j++) {
        if(s.charAt(j) == 'W') {
          if(j == 0) {
            w[j]++;
          } else {
            w[j] = w[j - 1] + 1;
            g[j] = g[j - 1];
            r[j] = r[j - 1];
          }
          w1 = j;
        } else if(s.charAt(j) == 'G') {
          if(j == 0) {
            g[j]++;
          } else {
            w[j] = w[j - 1];
            g[j] = g[j - 1] + 1;
            r[j] = r[j - 1]; 
          }
          g1 = j;
        } else {
          if(j == 0) {
            r[j]++;      
          } else {
            w[j] = w[j - 1];
            g[j] = g[j - 1];
            r[j] = r[j - 1] + 1; 
          }
        }
        if((w[j] < g[j]) || (g[j] < r[j])) p++;
      }
      String ans = "impossible";
      if((p == 0) && (g[len - 1] == r[len - 1]) && (w1 < g1)) ans = "possible";
      System.out.println(ans);
    }
  }
}
0