結果

問題 No.2021 Not A but B
ユーザー tktk_snsn
提出日時 2022-07-29 21:54:32
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 571 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 20,208 KB
最終ジャッジ日時 2024-07-19 14:26:52
合計ジャッジ時間 3,880 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

def RunLengthEncoding(S):
    res = []
    N = len(S)
    i = 0
    while i < N:
        cnt = 1
        while i < N - 1 and S[i] == S[i + 1]:
            cnt += 1
            i += 1
        res.append((S[i], cnt))
        i += 1
    return res


N = int(input())
S = input()


ans = 0
bb = 0

RLE = RunLengthEncoding(S)

for i, (k, v) in enumerate(RLE):
    if k == "A":
        ans += v - 1
        if v == 1:
            ans += 1
        else:
            ans += i > 0
            ans += i < len(RLE) - 1
    else:
        if v > 1:
            bb = 1

print(ans + bb)
0