結果

問題 No.154 市バス
ユーザー lam6er
提出日時 2025-04-16 00:20:13
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,037 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 81,768 KB
実行使用メモリ 76,244 KB
最終ジャッジ日時 2025-04-16 00:22:37
合計ジャッジ時間 1,498 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 2 MLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

T = int(input())
for _ in range(T):
    S = input().strip()
    count_g = S.count('G')
    count_r = S.count('R')
    if count_g != count_r:
        print("impossible")
        continue
    
    possible = True
    g_count = 0
    r_count = 0
    for c in S:
        if c == 'R':
            if g_count < (r_count + 1):
                possible = False
                break
            r_count += 1
        elif c == 'G':
            g_count += 1
    
    if not possible:
        print("impossible")
        continue
    
    # Check each G has at least one W before it
    prefix_w = [0] * (len(S) + 1)
    for i in range(len(S)):
        prefix_w[i+1] = prefix_w[i] + (1 if S[i] == 'W' else 0)
    
    for i in range(len(S)):
        if S[i] == 'G' and prefix_w[i] == 0:
            possible = False
            break
    
    if not possible:
        print("impossible")
        continue
    
    total_w = prefix_w[len(S)]
    if total_w < count_g:
        possible = False
    
    print("possible" if possible else "impossible")
0