結果
| 問題 |
No.2626 Similar But Different Name
|
| コンテスト | |
| ユーザー |
timi
|
| 提出日時 | 2024-02-10 10:58:54 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,476 ms / 3,000 ms |
| コード長 | 1,846 bytes |
| コンパイル時間 | 101 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 228,860 KB |
| 最終ジャッジ日時 | 2024-09-28 17:05:35 |
| 合計ジャッジ時間 | 40,900 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
N,M,K=map(int, input().split())
S=input()
T=input()
A,B=[],[]
for s in S:
if s.islower():
A.append(1)
else:
A.append(-1)
for s in T[::-1]:
if s.islower():
B.append(1)
else:
B.append(-1)
SS=S.lower()
TT=T.lower()
# 1-dimension Rolling Hash
class RollingHash():
def __init__(self, s, base, mod):
self.mod = mod
self.pw = pw = [1]*(len(s)+1)
l = len(s)
self.h = h = [0]*(l+1)
v = 0
for i in range(l):
h[i+1] = v = (v * base + ord(s[i])) % mod
v = 1
for i in range(l):
pw[i+1] = v = v * base % mod
def get(self, l, r):
return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod
base=37;mod = 10**9 +7
rs=RollingHash(SS,base,mod)
rt=RollingHash(TT,base,mod)
D=[];t=rt.get(0,M)
for i in range(N-M+1):
if t==rs.get(i,i+M):
D.append(i)
import numpy as np
def convolve(f, g):
"""多項式 f, g の積を計算する。
Parameters
----------
f : np.ndarray (int64)
f[i] に、x^i の係数が入っている
g : np.ndarray (int64)
g[i] に、x^i の係数が入っている
Returns
-------
h : np.ndarray
f,g の積
"""
# h の長さ以上の n=2^k を計算
fft_len = 1
while 2 * fft_len < len(f) + len(g) - 1:
fft_len *= 2
fft_len *= 2
# フーリエ変換
Ff = np.fft.rfft(f, fft_len)
Fg = np.fft.rfft(g, fft_len)
# 各点積
Fh = Ff * Fg
# フーリエ逆変換
h = np.fft.irfft(Fh, fft_len)
# 小数になっているので、整数にまるめる
h = np.rint(h).astype(np.int64)
return h[:len(f) + len(g) - 1]
f = np.array(A, np.int64)
g = np.array(B, np.int64)
H = list(convolve(f, g))
a,b=M-1-1,M-K-K
ans=0
for d in D:
c=H[d+M-1]
if b<=c<=a:
ans+=1
print(ans)
timi