⌨️

[AtCoder]ABC-372|C - Count ABC Again

2024/10/11に公開

設問ページ

提出結果

import re


def getIntMap():
    return map(int, input().split())


def getString():
    return input()


def getStringListRow(N):
    return [list(input().split()) for _ in range(N)]


def main():
    N, Q = getIntMap()
    S = [""] + list(getString())
    XC = getStringListRow(Q)

    # -> 初期の"ABC"を数える
    m = len(re.findall(r"ABC", "".join(S)))

    for X, C in XC:
        # -> 指定位置の前後2つまでを求める
        a = max(int(X) - 2, 1)
        b = min(int(X) + 2, len(S))

        t = "".join(S[a : b + 1])
        tm = 1 if "ABC" in t else 0

        # -> 指定位置を書き換えて同様に
        S[int(X)] = C

        u = "".join(S[a : b + 1])
        um = 1 if "ABC" in u else 0

        # -> 書き換えて変化があれば反映する
        m += 1 if tm < um else -1 if tm > um else 0

        print(m)


if __name__ == "__main__":
    main()

Discussion