結果

問題 No.154 市バス
ユーザー weizenweizen
提出日時 2018-03-24 19:04:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,617 bytes
コンパイル時間 135 ms
コンパイル使用メモリ 26,036 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 08:55:17
合計ジャッジ時間 825 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <string.h>

//WはG,Rのペアに一つづつ必要
//それ以外のWは最後に出てくるG,Rのペアに所属することにしてもんだいない

//最後から見ていったときにRの数>Gの数になっているか? => G,Rの順にペアが作れることが保証される
//先頭から見ていったときにWの数>Gの数>Rの数になっているか? => W,G,RとなるWが必ず選べる
//最後に出てくるGの後ろにWが入っていないか? => ****GWR, ****GRWのパターンを消す

int isPossibleOneLine(char * S){
    int len = strlen(S);
    if(len <= 3) return 0;
    int count_w = 0, count_g = 0, count_r = 0;
    int last_g = 0;
    
    //char possible = 1; 
    for(int i = len - 1; i >= 0; i--){
        if     (S[i] == 'R') count_r++;
        else if(S[i] == 'G'){
            count_g++;
            if(last_g <= i) last_g = i;
        }
        else /*S[i] == 'W'*/ count_w++;

        if(count_r < count_g) return 0;
    }
    
    count_w = 0; count_g = 0; count_r = 0;
    for(int i = 0; i < len; i++){
        if     (S[i] == 'R') count_r++;
        else if(S[i] == 'G') count_g++;
        else /*S[i] == 'W'*/ {
            count_w++;
            if(last_g < i) return 0;
        }

        if(count_w < count_g || count_w < count_r) return  0;
    }
    if(count_g == count_r) return 1;
    else return 0;
}

int main(){
    char S[1001];
    int N;
    scanf("%d",&N);
        
    while(N--){
        scanf("%s",S);
        if(isPossibleOneLine(S)) puts("possible");
        else                     puts("impossible");
    }
}
0