結果

問題 No.672 最長AB列
ユーザー roarisroaris
提出日時 2019-07-27 10:45:15
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 409 bytes
コンパイル時間 1,330 ms
コンパイル使用メモリ 10,660 KB
実行使用メモリ 52,620 KB
最終ジャッジ日時 2023-09-15 05:59:06
合計ジャッジ時間 3,257 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,308 KB
testcase_01 AC 18 ms
8,420 KB
testcase_02 AC 18 ms
8,424 KB
testcase_03 AC 18 ms
8,388 KB
testcase_04 AC 18 ms
8,420 KB
testcase_05 AC 18 ms
8,348 KB
testcase_06 AC 19 ms
8,320 KB
testcase_07 AC 18 ms
8,348 KB
testcase_08 AC 18 ms
8,452 KB
testcase_09 AC 249 ms
52,620 KB
testcase_10 AC 225 ms
33,744 KB
testcase_11 AC 223 ms
34,052 KB
testcase_12 AC 188 ms
11,940 KB
testcase_13 AC 198 ms
16,544 KB
testcase_14 AC 189 ms
12,340 KB
testcase_15 AC 195 ms
13,596 KB
testcase_16 AC 197 ms
14,508 KB
testcase_17 AC 230 ms
33,784 KB
testcase_18 AC 197 ms
10,160 KB
権限があれば一括ダウンロードができます

ソースコード

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