結果

問題 No.3015 アンチローリングハッシュ
ユーザー yadayada
提出日時 2020-03-06 22:01:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-04-22 07:43:51
合計ジャッジ時間 11,315 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 385 ms
13,696 KB
testcase_01 AC 381 ms
13,696 KB
testcase_02 AC 390 ms
13,696 KB
testcase_03 AC 386 ms
13,824 KB
testcase_04 AC 395 ms
13,696 KB
testcase_05 AC 388 ms
13,824 KB
testcase_06 AC 415 ms
18,688 KB
testcase_07 AC 400 ms
15,360 KB
testcase_08 AC 406 ms
19,712 KB
testcase_09 AC 405 ms
20,096 KB
testcase_10 AC 410 ms
19,968 KB
testcase_11 AC 415 ms
20,096 KB
testcase_12 AC 412 ms
20,224 KB
testcase_13 AC 410 ms
19,968 KB
testcase_14 AC 407 ms
19,712 KB
testcase_15 AC 411 ms
18,560 KB
testcase_16 AC 412 ms
20,224 KB
testcase_17 AC 414 ms
20,224 KB
testcase_18 AC 406 ms
20,224 KB
testcase_19 AC 406 ms
20,096 KB
testcase_20 AC 414 ms
19,968 KB
testcase_21 AC 409 ms
19,968 KB
testcase_22 AC 393 ms
13,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


class RollingHash:
    def __init__(self, seq, base=10 ** 9 + 7, mod=2 ** 89 - 1):
        """
        :param str|typing.Sequence[int] seq:
        :param int base:
        :param int mod:
        """
        if isinstance(seq, str):
            self._seq = seq = list(map(ord, seq))
        else:
            self._seq = seq
        self._size = len(seq)
        self._base = base
        self._mod = mod

        hashes = [0] * (len(seq) + 1)
        power = [1] * (len(seq) + 1)
        for i, c in enumerate(seq):
            hashes[i + 1] = (hashes[i] * base + c) % mod
            power[i + 1] = power[i] * base % mod
        self._hashes = hashes
        self._power = power

    def get(self, L, r):
        """
        [L, r) のハッシュ値を取得します
        :param int L:
        :param int r:
        """
        if L >= r:
            return 0
        return (self._hashes[r] - self._hashes[L] * self._power[r - L]) % self._mod


import random

A, B = list(map(int, sys.stdin.buffer.readline().split()))
S = ''
for _ in range(10 ** 5):
    S += chr(random.randint(ord('a'), ord('z')))

# 鳩の巣原理
rh = RollingHash(S, base=A, mod=B)
l = 0
r = 100
hist = {}
while r <= len(S):
    h = rh.get(l, r)
    if h in hist:
        pl, pr = hist[h]
        print(S[l:r])
        print(S[pl:pr])
        break
    hist[h] = l, r
    l += 1
    r += 1
else:
    assert False


0