結果

問題 No.2626 Similar But Different Name
ユーザー 👑 timitimi
提出日時 2024-02-10 10:58:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,389 ms / 3,000 ms
コード長 1,846 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 228,632 KB
最終ジャッジ日時 2024-02-10 10:59:34
合計ジャッジ時間 38,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 476 ms
42,848 KB
testcase_01 AC 443 ms
42,864 KB
testcase_02 AC 453 ms
42,840 KB
testcase_03 AC 469 ms
42,852 KB
testcase_04 AC 445 ms
42,852 KB
testcase_05 AC 442 ms
42,868 KB
testcase_06 AC 440 ms
42,868 KB
testcase_07 AC 469 ms
43,496 KB
testcase_08 AC 452 ms
43,496 KB
testcase_09 AC 450 ms
43,520 KB
testcase_10 AC 482 ms
43,564 KB
testcase_11 AC 451 ms
43,560 KB
testcase_12 AC 447 ms
43,552 KB
testcase_13 AC 461 ms
43,312 KB
testcase_14 AC 492 ms
43,536 KB
testcase_15 AC 453 ms
43,528 KB
testcase_16 AC 474 ms
43,564 KB
testcase_17 AC 458 ms
43,552 KB
testcase_18 AC 1,352 ms
228,632 KB
testcase_19 AC 1,371 ms
155,252 KB
testcase_20 AC 1,357 ms
155,252 KB
testcase_21 AC 1,098 ms
135,700 KB
testcase_22 AC 1,389 ms
203,744 KB
testcase_23 AC 1,363 ms
204,180 KB
testcase_24 AC 1,383 ms
198,504 KB
testcase_25 AC 1,368 ms
200,432 KB
testcase_26 AC 1,378 ms
204,120 KB
testcase_27 AC 1,351 ms
204,604 KB
testcase_28 AC 1,352 ms
201,840 KB
testcase_29 AC 1,278 ms
191,852 KB
testcase_30 AC 1,318 ms
194,296 KB
testcase_31 AC 1,304 ms
197,608 KB
testcase_32 AC 1,326 ms
199,312 KB
testcase_33 AC 1,302 ms
199,608 KB
testcase_34 AC 1,349 ms
192,008 KB
testcase_35 AC 1,328 ms
197,692 KB
testcase_36 AC 1,335 ms
192,008 KB
testcase_37 AC 1,323 ms
228,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0