結果

問題 No.154 市バス
ユーザー kenji_shioyakenji_shioya
提出日時 2016-06-26 23:14:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 584 ms / 2,000 ms
コード長 1,670 bytes
コンパイル時間 4,647 ms
コンパイル使用メモリ 80,852 KB
実行使用メモリ 47,572 KB
最終ジャッジ日時 2024-04-21 09:48:04
合計ジャッジ時間 7,479 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 478 ms
46,544 KB
testcase_01 AC 437 ms
46,840 KB
testcase_02 AC 446 ms
46,564 KB
testcase_03 AC 440 ms
47,180 KB
testcase_04 AC 474 ms
46,468 KB
testcase_05 AC 119 ms
40,968 KB
testcase_06 AC 121 ms
41,040 KB
testcase_07 AC 584 ms
47,572 KB
testcase_08 AC 125 ms
41,404 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