結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー tamato
提出日時 2021-10-29 22:42:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 553 ms / 4,000 ms
コード長 2,509 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 119,464 KB
最終ジャッジ日時 2024-10-07 12:24:07
合計ジャッジ時間 14,228 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    X = input().rstrip('\n')
    Y = input().rstrip('\n')
    NX = len(X)
    NY = len(Y)
    NXY = NX + NY

    cs_X = [[0] * (NX+1) for _ in range(26)]
    for i in range(NX):
        x = X[i]
        for j in range(26):
            c = chr(j + 97)
            if c == x:
                cs_X[j][i+1] = cs_X[j][i] + 1
            else:
                cs_X[j][i+1] = cs_X[j][i]
    cs_Y = [[0] * (NY+1) for _ in range(26)]
    for i in range(NY):
        y = Y[i]
        for j in range(26):
            c = chr(j + 97)
            if c == y:
                cs_Y[j][i+1] = cs_Y[j][i] + 1
            else:
                cs_Y[j][i+1] = cs_Y[j][i]

    def calc(i, c):
        j = ord(c) - 97
        p = i // NXY
        res = p * (cs_X[j][-1] + cs_Y[j][-1])
        q = i % NXY

        if q == 0:
            return res

        # X
        if p & 1:
            # rev
            res += cs_X[j][NX] - cs_X[j][max(0, NX - q)]
        else:
            res += cs_X[j][min(q, NX)]

        # Y
        if q > NX:
            ppp = p + 1
            cnt = 0
            while True:
                r = 1 << (ppp.bit_length())
                ppp_new = r - ppp
                if ppp_new == ppp:
                    break
                ppp = ppp_new
                cnt += 1
            qq = q - NX
            if cnt & 1:
                # rev
                res += cs_Y[j][NY] - cs_Y[j][NY - qq]
            else:
                res += cs_Y[j][qq]
        return res

    """
    S = X
    for _ in range(5):
        S_new = S[:] + Y + S[::-1]
        S = S_new
    print(S, len(S))
    NS = len(S)
    cs_S = [[0] * (NS + 1) for _ in range(26)]
    for i in range(NS):
        s = S[i]
        for j in range(26):
            c = chr(j + 97)
            if c == s:
                cs_S[j][i + 1] = cs_S[j][i] + 1
            else:
                cs_S[j][i + 1] = cs_S[j][i]
    """

    Q = int(input())
    for _ in range(Q):
        l, r, c = input().split()
        l = int(l)
        r = int(r)
        ans = calc(r, c) - calc(l-1, c)
        print(calc(r, c) - calc(l-1, c))
        """
        ans2 = cs_S[ord(c) - 97][r] - cs_S[ord(c) - 97][l-1]
        if ans != ans2:
            print(ans)
            print(ans2)
            print(l, r)
            print(calc(r, c), cs_S[ord(c) - 97][r])
            print(calc(l-1, c), cs_S[ord(c) - 97][l-1])
        """


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