結果

問題 No.154 市バス
ユーザー takeya_okino
提出日時 2019-08-02 23:00:41
言語 Java
(openjdk 23)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 1,270 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 77,588 KB
実行使用メモリ 47,912 KB
最終ジャッジ日時 2024-07-05 08:19:38
合計ジャッジ時間 6,055 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

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