結果
| 問題 | No.3667 Prefix Count Queries |
| ユーザー |
回転
|
| 提出日時 | 2026-08-31 21:27:20 |
| 言語 | PyPy3 (7.3.23) |
| 結果 |
AC
|
| 実行時間 | 465 ms / 2,000 ms |
| + 467µs | |
| コード長 | 2,631 bytes |
| 記録 | |
| コンパイル時間 | 235 ms |
| コンパイル使用メモリ | 96,208 KB |
| 実行使用メモリ | 143,636 KB |
| 最終ジャッジ日時 | 2026-09-01 00:52:03 |
| 合計ジャッジ時間 | 15,022 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
"""RollingHash
・つかい方
rh = RollingHash(S): インスタンス生成
rh.get(l, r): hash(S[l, r))
・めも
1つ目のインスタンス生成で係数cfを決めてしまうから、
たくさんインスタンスつくっても整合性がとれてる。
・参考URL
https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
"""
import random
class RollingHash:
def __init__(self, S: str):
le = len(S)
if RConst.cf == 0:
RConst.cf = random.randrange(1 << 31, RConst.Mod)
if len(RConst.rui_cf) <= 2*10**5+100:
RConst.make_rui_cf(2*10**5+100)
self.hash_arr = [1]
for el in S:
self.hash_arr.append(RConst.calc_mod(
RConst.mul(self.hash_arr[-1], RConst.cf) + ord(el)))
def append(self, s:str):
self.hash_arr.append(RConst.calc_mod(
RConst.mul(self.hash_arr[-1], RConst.cf) + ord(s)))
def pop(self):
self.hash_arr.pop()
def get(self, l: int, r: int) -> int:
"hash(S[l..r))"
return RConst.sub(self.hash_arr[r],
RConst.mul(self.hash_arr[l], RConst.rui_cf[r - l]))
def __len__(self):
return len(self.hash_arr) - 1
class RConst:
Mask30 = (1 << 30) - 1
Mask31 = (1 << 31) - 1
Mod = (1 << 61) - 1
cf = 0
rui_cf = [1]
@staticmethod
def calc_mod(x: int) -> int:
xu, xd = x >> 61, x & RConst.Mod
ret = xu + xd
if RConst.Mod <= ret:
ret -= RConst.Mod
return ret
@staticmethod
def sub(a: int, b: int) -> int:
if a < b:
return a + RConst.Mod - b
return a - b
@staticmethod
def mul(a: int, b: int) -> int:
au, ad = a >> 31, a & RConst.Mask31
bu, bd = b >> 31, b & RConst.Mask31
mid = ad * bu + au * bd
midu, midd = mid >> 30, mid & RConst.Mask30
return RConst.calc_mod(((au * bu) << 1) + midu + (midd << 31) + ad * bd)
@staticmethod
def make_rui_cf(x: int):
l = len(RConst.rui_cf)
for _ in range(x - l + 1):
RConst.rui_cf.append(RConst.mul(RConst.cf, RConst.rui_cf[-1]))
from collections import defaultdict
N = int(input())
A = [input() for _ in range(N)]
d = defaultdict(int)
for i in range(N):
rh = RollingHash(A[i])
for j in range(len(A[i])+1):
d[rh.get(0,j)] += 1
RH = RollingHash("")
Q = int(input())
for _ in range(Q):
query = input().split()
if(query[0] == "1"):
_,x = query
RH.append(x)
elif(query[0] == "2"):
RH.pop()
else:
print(d[RH.get(0,len(RH))])
回転