結果

問題 No.154 市バス
ユーザー Konton7Konton7
提出日時 2020-01-18 18:23:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,679 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 204,432 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 08:16:25
合計ジャッジ時間 3,092 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define DEBUG

void is_possible(string s){
    stack<int> bus, bus2;
    reverse(s.begin(), s.end());
    string result = "possible";
    // cout << s << endl;
    bool white_ok = false;
    rep(i, s.length()){
        if (white_ok){

            if (s[i] == 'R')
                bus.push(1);
            else if (s[i] == 'G'){
                bus2.push(1);
                if (!bus.empty())
                    bus.pop();
                else{
                    result = "impossible";
                    break;
                }
            }
            else if (s[i] == 'W' && !bus2.empty()){
                bus2.pop();
            }
        }
        else{
            
            if (s[i] == 'R'){
                bus.push(1);
                
            }
            else if (s[i] == 'G'){
                bus2.push(1);
                if (!bus.empty()){
                    bus.pop();
                    white_ok = true;
                }
                else{
                    result = "impossible";
                    break;
                }
            }
            else if (s[i] == 'W'){
                    result = "impossible";
                    break;
                }
        }
    }
    
    if (!bus.empty() || !bus2.empty())
        result = "impossible";
    cout << result << endl;
    return;
}

int main()
{
    int n;
    string s;
    cin >> n;
    
    rep(i, n){
        cin >> s;
        is_possible(s);
    }        
        
    
    
    return 0;
}
0