結果

問題 No.154 市バス
ユーザー gew1fw
提出日時 2025-06-12 20:49:23
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 983 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 76,416 KB
最終ジャッジ日時 2025-06-12 20:51:07
合計ジャッジ時間 1,625 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 2 MLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

T = int(input())
for _ in range(T):
    S = input().strip()
    g_count = 0
    r_count = 0
    possible = True
    
    # Check if the number of G and R are equal
    g_total = S.count('G')
    r_total = S.count('R')
    if g_total != r_total:
        print("impossible")
        continue
    
    # Check each G has at least one W before it
    has_w = False
    for c in S:
        if c == 'W':
            has_w = True
        elif c == 'G':
            if not has_w:
                possible = False
                break
    
    if not possible:
        print("impossible")
        continue
    
    # Check R's and G's order using a stack-like approach
    current_g = 0
    for c in S:
        if c == 'G':
            current_g += 1
        elif c == 'R':
            if current_g <= 0:
                possible = False
                break
            current_g -= 1
    
    if possible and current_g == 0:
        print("possible")
    else:
        print("impossible")
0