結果

問題 No.672 最長AB列
ユーザー FromBooskaFromBooska
提出日時 2023-02-22 15:28:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 607 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 130,688 KB
最終ジャッジ日時 2023-09-29 23:08:14
合計ジャッジ時間 5,799 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,388 KB
testcase_01 AC 104 ms
71,428 KB
testcase_02 AC 98 ms
71,348 KB
testcase_03 AC 99 ms
71,604 KB
testcase_04 AC 98 ms
71,472 KB
testcase_05 AC 99 ms
71,432 KB
testcase_06 AC 97 ms
71,608 KB
testcase_07 AC 99 ms
71,384 KB
testcase_08 AC 101 ms
71,352 KB
testcase_09 AC 251 ms
130,688 KB
testcase_10 AC 148 ms
94,108 KB
testcase_11 AC 163 ms
99,164 KB
testcase_12 AC 131 ms
88,652 KB
testcase_13 AC 131 ms
88,568 KB
testcase_14 AC 132 ms
90,252 KB
testcase_15 AC 132 ms
88,684 KB
testcase_16 AC 131 ms
88,680 KB
testcase_17 AC 151 ms
95,972 KB
testcase_18 AC 131 ms
91,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 二重ループはできない, 2*10**5
# 入力例1,AAABBA。+1 for A, -1 for Bとすれば
# levels = [0, 1, 2, 3, 2, 1, 2]
# 同じ数字で一番離れている区間が答え

S = input()
levels = [0]
for s in S:
    if s == 'A':
        levels.append(levels[-1]+1)
    if s == 'B':
        levels.append(levels[-1]-1)
        
#print(levels)

from collections import defaultdict
dic = defaultdict(list)
for i in range(len(levels)):
    dic[levels[i]].append(i)

#print(dic)

ans = 0
for d in dic:
    if len(dic[d]) >= 2:
        calc = dic[d][-1]-dic[d][0]
        ans = max(ans, calc)
print(ans)
0