結果

問題 No.672 最長AB列
ユーザー alexara1123alexara1123
提出日時 2019-09-21 10:53:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 585 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 115,912 KB
最終ジャッジ日時 2023-10-17 14:30:46
合計ジャッジ時間 2,451 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,328 KB
testcase_01 AC 42 ms
53,328 KB
testcase_02 AC 42 ms
53,328 KB
testcase_03 AC 42 ms
53,328 KB
testcase_04 AC 42 ms
53,328 KB
testcase_05 AC 42 ms
53,328 KB
testcase_06 AC 42 ms
53,328 KB
testcase_07 AC 42 ms
53,328 KB
testcase_08 AC 43 ms
53,328 KB
testcase_09 AC 119 ms
115,912 KB
testcase_10 AC 107 ms
101,404 KB
testcase_11 AC 107 ms
100,348 KB
testcase_12 AC 84 ms
88,636 KB
testcase_13 AC 82 ms
88,636 KB
testcase_14 AC 83 ms
89,260 KB
testcase_15 AC 83 ms
89,260 KB
testcase_16 AC 83 ms
88,636 KB
testcase_17 AC 105 ms
100,348 KB
testcase_18 AC 73 ms
88,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, Counter

def main():
    s = list(input())
    n = len(s)
    
    l = [0] * (n)
    if s[0] == "A":
        l[0] = 1
    else:
        l[0] = -1
    for i in range(1, n):
        if s[i] == "A":
            l[i] = l[i-1] + 1
        else:
            l[i] = l[i - 1] - 1
    l = [0] + l

    d = defaultdict(list)
    for i in range(n+1):
        d[l[i]].append(i)

    ans = 0
    for k in d.keys():
        if len(d[k]) <= 1:
            continue
        ans = max(ans, d[k][-1] - d[k][0])
    print(ans)


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