結果

問題 No.154 市バス
ユーザー kenji_shioyakenji_shioya
提出日時 2016-06-26 22:56:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,649 bytes
コンパイル時間 4,498 ms
コンパイル使用メモリ 77,316 KB
実行使用メモリ 47,956 KB
最終ジャッジ日時 2024-04-21 09:47:46
合計ジャッジ時間 8,125 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 119 ms
41,120 KB
testcase_06 AC 114 ms
40,960 KB
testcase_07 WA -
testcase_08 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercise129{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    for(int i = 0; i < n; i++){
      judge(sc.next());
    }

	}
  private static void judge(String s){
    char[] sa = s.toCharArray();
    if(sa.length < 3){
      impossible();
      return;
    }
    if(sa[sa.length - 1] != 'R' || sa[sa.length - 2] == 'W'){
      impossible();
      return;
    }
    int[] count = new int[3];
    boolean hasG = false;
    for(int i = 0; i < sa.length; i++){
      if(sa[i] =='R'){
        count[0]++;
      }else if(sa[i] == 'G'){
        hasG = true;
        count[1]++;
      }else if(sa[i] == 'W' && !hasG){
        impossible();
        return;
      }
    }
    if(count[0] != count[1]){
      impossible();
      return;
    }
    for(int i = sa.length - 1; i >= 0; i--){
      if(sa[i] == 'R'){
        boolean isGExist = false;
        for(int j = i - 1; j >= 0; j--){
          if(sa[j] == 'G'){
            isGExist = true;
            sa[j] = 'x';
            boolean isWExist = false;
            for(int k = j - 1; k >= 0; k--){
              if(sa[k] == 'W'){
                isWExist = true;
                sa[k] = 'x';
                break;
              }else if(k == 0 && !isWExist){
                impossible();
                return;
              }
            }
            break;
          }else if(j == 0 && !isGExist){
            impossible();
            return;
          }
        }
      }
    }
    System.out.println("possible");
  }
  private static void impossible(){
    System.out.println("impossible");
  }
}
0