結果

問題 No.672 最長AB列
ユーザー roaris
提出日時 2019-07-27 10:45:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 409 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 54,884 KB
最終ジャッジ日時 2024-07-02 10:49:05
合計ジャッジ時間 4,005 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

S = input()
mark_dict = defaultdict(list)
cnt = [0]
mark_dict[0].append(0)
ans = 0

for i in range(len(S)):
    if S[i] == 'A':
        cnt_i = cnt[-1] + 1
    else:
        cnt_i = cnt[-1] - 1
    
    cnt.append(cnt_i)
    
    if len(mark_dict[cnt_i]) == 0:
        mark_dict[cnt_i].append(i+1)
    else:
        ans = max(ans, i - mark_dict[cnt_i][0] + 1)

print(ans)
0