結果

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

ソースコード

diff #

T = int(input())
for _ in range(T):
    s = input().strip()
    possible = True
    
    # Check first character is not R
    if s[0] == 'R':
        print("impossible")
        continue
    
    # Check last character is not G
    if s[-1] == 'G':
        print("impossible")
        continue
    
    # Precompute has_w_before array
    n = len(s)
    has_w_before = [False] * (n + 1)
    for i in range(n):
        has_w_before[i+1] = has_w_before[i] or (s[i] == 'W')
    
    # Check each G has at least one W before it
    for i in range(n):
        if s[i] == 'G' and not has_w_before[i]:
            possible = False
            break
    
    if not possible:
        print("impossible")
        continue
    
    # Check count of G and R are equal
    count_g = s.count('G')
    count_r = s.count('R')
    if count_g != count_r:
        print("impossible")
        continue
    
    # Check balance during processing
    balance = 0
    for c in s:
        if c == 'G':
            balance += 1
        elif c == 'R':
            balance -= 1
            if balance < 0:
                possible = False
                break
    
    if possible and balance == 0:
        print("possible")
    else:
        print("impossible")
0