提出 #66638314


ソースコード 拡げる

import sys
input = sys.stdin.read

MOD = 10**9 + 7
BASE = 31

def main():
    data = input().split()
    idx = 0

    N = int(data[idx]); idx += 1
    Q = int(data[idx]); idx += 1
    S = data[idx]; idx += 1

    # 前処理:prefix hashとbaseの累乗を計算
    hash_vals = [0] * (N + 1)
    power = [1] * (N + 1)

    for i in range(N):
        code = ord(S[i]) - ord('a') + 1
        hash_vals[i + 1] = (hash_vals[i] * BASE + code) % MOD
        power[i + 1] = (power[i] * BASE) % MOD

    def get_hash(l, r):
        # l, r は1-indexed
        return (hash_vals[r] - hash_vals[l - 1] * power[r - l + 1] % MOD + MOD) % MOD

    output = []
    for _ in range(Q):
        a = int(data[idx]); idx += 1
        b = int(data[idx]); idx += 1
        c = int(data[idx]); idx += 1
        d = int(data[idx]); idx += 1

        if get_hash(a, b) == get_hash(c, d):
            output.append("Yes")
        else:
            output.append("No")

    print("\n".join(output))

main()

提出情報

提出日時
問題 A56 - String Hash
ユーザ myoshizumi
言語 Python (CPython 3.11.4)
得点 1000
コード長 1025 Byte
結果 AC
実行時間 482 ms
メモリ 84100 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 1000 / 1000
結果
AC × 1
AC × 11
セット名 テストケース
Sample sample_01.txt
All killer_01.txt, max_01.txt, max_02.txt, max_03.txt, min_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, sample_01.txt
ケース名 結果 実行時間 メモリ
killer_01.txt AC 447 ms 83684 KiB
max_01.txt AC 262 ms 59168 KiB
max_02.txt AC 281 ms 79968 KiB
max_03.txt AC 280 ms 77368 KiB
min_01.txt AC 10 ms 9016 KiB
random_01.txt AC 482 ms 83696 KiB
random_02.txt AC 396 ms 84100 KiB
random_03.txt AC 384 ms 84004 KiB
random_04.txt AC 382 ms 84016 KiB
random_05.txt AC 380 ms 83848 KiB
sample_01.txt AC 11 ms 8944 KiB