結果

問題 No.2398 ヒドラ崩し
ユーザー gew1fw
提出日時 2025-06-12 18:01:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 864 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,220 KB
実行使用メモリ 71,280 KB
最終ジャッジ日時 2025-06-12 18:01:47
合計ジャッジ時間 2,170 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    s = sys.stdin.readline().strip()
    n = len(s)
    if n == 0:
        print(-1)
        return
    
    # Precompute matching parentheses positions
    match = [0] * n
    stack = []
    for i in range(n):
        if s[i] == '(':
            stack.append(i)
        else:
            if stack:
                j = stack.pop()
                match[j] = i
                match[i] = j
    
    # dp[i] will store the Grundy number for the substring starting at index i
    dp = [0] * (n + 1)
    
    for i in range(n - 1, -1, -1):
        if s[i] == '(':
            j = match[i]
            inner_grundy = dp[i + 1]
            rest_grundy = dp[j + 1]
            dp[i] = (inner_grundy + 1) ^ rest_grundy
    
    grundy = dp[0]
    if grundy != 0:
        print(0)
    else:
        print(1)

if __name__ == '__main__':
    main()
0