結果

問題 No.154 市バス
ユーザー kenji_shioyakenji_shioya
提出日時 2016-06-26 23:14:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 622 ms / 2,000 ms
コード長 1,670 bytes
コンパイル時間 3,359 ms
コンパイル使用メモリ 74,388 KB
実行使用メモリ 60,772 KB
最終ジャッジ日時 2023-08-03 11:52:30
合計ジャッジ時間 7,850 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 458 ms
60,332 KB
testcase_01 AC 455 ms
60,016 KB
testcase_02 AC 424 ms
60,772 KB
testcase_03 AC 458 ms
60,112 KB
testcase_04 AC 467 ms
60,256 KB
testcase_05 AC 125 ms
55,788 KB
testcase_06 AC 123 ms
55,824 KB
testcase_07 AC 622 ms
60,768 KB
testcase_08 AC 127 ms
55,768 KB
権限があれば一括ダウンロードができます

ソースコード

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' || sa[0] != 'W'){
      impossible();
      return;
    }
    int[] count = new int[3];
    boolean hasG = false;
    for(int i = sa.length - 1; i >= 0; 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