結果

問題 No.2626 Similar But Different Name
ユーザー titiatitia
提出日時 2024-02-10 00:32:03
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,433 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 201,840 KB
最終ジャッジ日時 2024-02-10 00:32:28
合計ジャッジ時間 22,568 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 32 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 59 ms
73,428 KB
testcase_10 AC 76 ms
76,488 KB
testcase_11 AC 80 ms
76,348 KB
testcase_12 AC 78 ms
76,760 KB
testcase_13 AC 74 ms
76,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 454 ms
137,752 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 1,190 ms
169,088 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M,K=map(int,input().split())
S=input().strip()
T=input().strip()

A=[]
B=[]
SA=[]
SB=[]
for s in S:
    if ord(s)<97:
        A.append(1)
        SA.append(ord(s)-65)
    else:
        A.append(998244352)
        SA.append(ord(s)-97)

for t in T:
    if ord(t)<97:
        B.append(1)
        SB.append(ord(t)-65)
    else:
        B.append(998244352)
        SB.append(ord(t)-97)

# FFT
# mod=998244353 における、NTTによる高速フーリエ変換、畳み込み
# 他の提出を参考にしており、あまり理解できていません……
mod=998244353
 
Weight=[1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129, 733596141, 267099868, 15311432, 0]
Weight_inv=[1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366, 428961804, 382752275, 469870224, 0]
 
def fft(A,n,h,inverse=0):
 
    if inverse==0:
    
        for i in range(h):
            m=1<<(h-i-1)
            for j in range(1<<i):
                w=1     
                ij=j*m*2
                
                wk=Weight[h-i]
                    
                for k in range(m):
                    A[ij+k],A[ij+k+m]=(A[ij+k]+A[ij+k+m])%mod,(A[ij+k]-A[ij+k+m])*w%mod
                    w=w*wk%mod
    else:
        for i in range(h):
            m=1<<i
            for j in range(1<<(h-i-1)):
                w=1     
                ij=j*m*2
                
                wk=Weight_inv[i+1]
                    
                for k in range(m):
                    A[ij+k],A[ij+k+m]=(A[ij+k]+A[ij+k+m]*w)%mod,(A[ij+k]-A[ij+k+m]*w)%mod
                    w=w*wk%mod
        
 
    if inverse==1:
        INV_n=pow(n,mod-2,mod)
        for i in range(n):
            A[i]=A[i]*INV_n%mod
 
    return A
 
def convolution(A,B):
    
    FFTLEN=len(A)+len(B)-1
    h=FFTLEN.bit_length()
    LEN=2**h
 
    A+=[0]*(LEN-len(A)) # A,Bのサイズを2ベキに揃える
    B+=[0]*(LEN-len(B))
 
    A_FFT=fft(A,LEN,h)
    B_FFT=fft(B,LEN,h)
 
    for i in range(len(A)):
        A[i]=A[i]*B[i]%mod
 
    A=fft(A,LEN,h,1)
 
    return A[:FFTLEN]

X=convolution(A,B[::-1])

# Rolling Hash
LEN=len(S)

p=26 # 文字の種類
mod=67280421310721 # Hashがぶつからない, pと互いに素な数を適当に指定

TABLE=[0] # Rolling Hashのテーブル. 最初は0

for i in range(LEN):
    TABLE.append((p*TABLE[-1]%mod+SA[i])%mod) # テーブルを埋める

def hash_ij(i,j): # [i,j)のハッシュ値を求める
    return (TABLE[j]-TABLE[i]*pow(p,j-i,mod))%mod

LEN=len(T)

p=26 # 文字の種類
mod=67280421310721 # Hashがぶつからない, pと互いに素な数を適当に指定

TABLE2=[0] # Rolling Hashのテーブル. 最初は0

for i in range(LEN):
    TABLE2.append(p*TABLE2[-1]%mod+SB[i]%mod) # テーブルを埋める

def hash_ij2(i,j): # [i,j)のハッシュ値を求める
    return (TABLE2[j]-TABLE2[i]*pow(p,j-i,mod))%mod

ANS=0
for i in range(N-M+1):
    if hash_ij(i,i+M)== hash_ij2(0,M):
        #print(X[i])
        if M-2*K<=X[i+M]<M:
            ANS+=1
        elif M-2*K<0 and (M-2*K)%mod<=X[i+M]:
            ANS+=1

print(ANS)
        
0