結果

問題 No.154 市バス
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-09-25 12:37:01
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,202 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 53,404 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-07-27 10:48:15
合計ジャッジ時間 1,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
using namespace std;

int solve(string str){
    // 最後から2本目より前のバスが走っている可能性のある系統の最大数と最少数
    int max_n = 0;
    int min_n = 0;
    // 最後から一本目が走ったが、最後の一本は走っていない系統の数
    int last = 0;
    FOR(j, 0, str.size()){
        char c = str[j];
        if (c == 'W'){
            max_n++;
            min_n = 1;
        } else if (c == 'G'){
            max_n--;
            min_n = 0;
            if (max_n < 0) return 0;
            last += 1;
        } else{
            last--;
            if (last < 0) return 0;
        }
    }
    // lastが0かつRで終わる場合のみあり得るパターン
    if (last == 0 && min_n == 0 && str[-1] == 'R'){
        return 1;
    } else {
        return 0;
    }
}

int main(){
    int t;
    cin >> t;
    FOR(i, 0, t){
        string str;
        cin >> str;
        int if_possible = solve(str);
        if(if_possible){
            cout << "possible" << endl;
        } else {
            cout << "impossible" << endl;
        }
    }

    return 0;
}
0