結果

問題 No.2626 Similar But Different Name
ユーザー hato336hato336
提出日時 2024-02-10 08:53:32
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,618 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 139,464 KB
最終ジャッジ日時 2024-09-28 17:02:24
合計ジャッジ時間 58,098 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
89,060 KB
testcase_01 AC 119 ms
89,116 KB
testcase_02 AC 120 ms
89,192 KB
testcase_03 AC 132 ms
88,992 KB
testcase_04 AC 124 ms
89,128 KB
testcase_05 AC 120 ms
89,140 KB
testcase_06 AC 123 ms
89,000 KB
testcase_07 AC 163 ms
90,096 KB
testcase_08 AC 158 ms
90,096 KB
testcase_09 AC 147 ms
90,052 KB
testcase_10 AC 193 ms
91,496 KB
testcase_11 AC 186 ms
90,924 KB
testcase_12 AC 179 ms
90,748 KB
testcase_13 AC 193 ms
91,308 KB
testcase_14 AC 183 ms
91,524 KB
testcase_15 AC 197 ms
91,500 KB
testcase_16 AC 181 ms
91,184 KB
testcase_17 AC 185 ms
90,956 KB
testcase_18 AC 1,846 ms
136,108 KB
testcase_19 AC 2,413 ms
102,904 KB
testcase_20 AC 2,403 ms
102,712 KB
testcase_21 AC 2,442 ms
110,100 KB
testcase_22 AC 2,813 ms
138,192 KB
testcase_23 AC 2,736 ms
138,328 KB
testcase_24 AC 2,970 ms
138,424 KB
testcase_25 AC 2,878 ms
138,876 KB
testcase_26 AC 2,793 ms
138,108 KB
testcase_27 AC 2,779 ms
138,044 KB
testcase_28 AC 2,885 ms
138,104 KB
testcase_29 AC 2,958 ms
139,408 KB
testcase_30 AC 2,844 ms
138,136 KB
testcase_31 AC 2,793 ms
138,212 KB
testcase_32 AC 2,715 ms
138,048 KB
testcase_33 AC 2,755 ms
138,408 KB
testcase_34 TLE -
testcase_35 AC 2,829 ms
137,964 KB
testcase_36 TLE -
testcase_37 AC 1,869 ms
135,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
class FFT():
    def primitive_root_constexpr(self,m):
        if m==2:return 1
        if m==167772161:return 3
        if m==469762049:return 3
        if m==754974721:return 11
        if m==998244353:return 3
        divs=[0]*20
        divs[0]=2
        cnt=1
        x=(m-1)//2
        while(x%2==0):x//=2
        i=3
        while(i*i<=x):
            if (x%i==0):
                divs[cnt]=i
                cnt+=1
                while(x%i==0):
                    x//=i
            i+=2
        if x>1:
            divs[cnt]=x
            cnt+=1
        g=2
        while(1):
            ok=True
            for i in range(cnt):
                if pow(g,(m-1)//divs[i],m)==1:
                    ok=False
                    break
            if ok:
                return g
            g+=1
    def bsf(self,x):
        res=0
        while(x%2==0):
            res+=1
            x//=2
        return res
    rank2=0
    root=[]
    iroot=[]
    rate2=[]
    irate2=[]
    rate3=[]
    irate3=[]
    
    def __init__(self,MOD):
        self.mod=MOD
        self.g=self.primitive_root_constexpr(self.mod)
        self.rank2=self.bsf(self.mod-1)
        self.root=[0 for i in range(self.rank2+1)]
        self.iroot=[0 for i in range(self.rank2+1)]
        self.rate2=[0 for i in range(self.rank2)]
        self.irate2=[0 for i in range(self.rank2)]
        self.rate3=[0 for i in range(self.rank2-1)]
        self.irate3=[0 for i in range(self.rank2-1)]
        self.root[self.rank2]=pow(self.g,(self.mod-1)>>self.rank2,self.mod)
        self.iroot[self.rank2]=pow(self.root[self.rank2],self.mod-2,self.mod)
        for i in range(self.rank2-1,-1,-1):
            self.root[i]=(self.root[i+1]**2)%self.mod
            self.iroot[i]=(self.iroot[i+1]**2)%self.mod
        prod=1;iprod=1
        for i in range(self.rank2-1):
            self.rate2[i]=(self.root[i+2]*prod)%self.mod
            self.irate2[i]=(self.iroot[i+2]*iprod)%self.mod
            prod=(prod*self.iroot[i+2])%self.mod
            iprod=(iprod*self.root[i+2])%self.mod
        prod=1;iprod=1
        for i in range(self.rank2-2):
            self.rate3[i]=(self.root[i+3]*prod)%self.mod
            self.irate3[i]=(self.iroot[i+3]*iprod)%self.mod
            prod=(prod*self.iroot[i+3])%self.mod
            iprod=(iprod*self.root[i+3])%self.mod
    def butterfly(self,a):
        n=len(a)
        h=(n-1).bit_length()
        
        LEN=0
        while(LEN<h):
            if (h-LEN==1):
                p=1<<(h-LEN-1)
                rot=1
                for s in range(1<<LEN):
                    offset=s<<(h-LEN)
                    for i in range(p):
                        l=a[i+offset]
                        r=a[i+offset+p]*rot
                        a[i+offset]=(l+r)%self.mod
                        a[i+offset+p]=(l-r)%self.mod
                    rot*=self.rate2[(~s & -~s).bit_length()-1]
                    rot%=self.mod
                LEN+=1
            else:
                p=1<<(h-LEN-2)
                rot=1
                imag=self.root[2]
                for s in range(1<<LEN):
                    rot2=(rot*rot)%self.mod
                    rot3=(rot2*rot)%self.mod
                    offset=s<<(h-LEN)
                    for i in range(p):
                        a0=a[i+offset]
                        a1=a[i+offset+p]*rot
                        a2=a[i+offset+2*p]*rot2
                        a3=a[i+offset+3*p]*rot3
                        a1na3imag=(a1-a3)%self.mod*imag
                        a[i+offset]=(a0+a2+a1+a3)%self.mod
                        a[i+offset+p]=(a0+a2-a1-a3)%self.mod
                        a[i+offset+2*p]=(a0-a2+a1na3imag)%self.mod
                        a[i+offset+3*p]=(a0-a2-a1na3imag)%self.mod
                    rot*=self.rate3[(~s & -~s).bit_length()-1]
                    rot%=self.mod
                LEN+=2
                
    def butterfly_inv(self,a):
        n=len(a)
        h=(n-1).bit_length()
        LEN=h
        while(LEN):
            if (LEN==1):
                p=1<<(h-LEN)
                irot=1
                for s in range(1<<(LEN-1)):
                    offset=s<<(h-LEN+1)
                    for i in range(p):
                        l=a[i+offset]
                        r=a[i+offset+p]
                        a[i+offset]=(l+r)%self.mod
                        a[i+offset+p]=(l-r)*irot%self.mod
                    irot*=self.irate2[(~s & -~s).bit_length()-1]
                    irot%=self.mod
                LEN-=1
            else:
                p=1<<(h-LEN)
                irot=1
                iimag=self.iroot[2]
                for s in range(1<<(LEN-2)):
                    irot2=(irot*irot)%self.mod
                    irot3=(irot*irot2)%self.mod
                    offset=s<<(h-LEN+2)
                    for i in range(p):
                        a0=a[i+offset]
                        a1=a[i+offset+p]
                        a2=a[i+offset+2*p]
                        a3=a[i+offset+3*p]
                        a2na3iimag=(a2-a3)*iimag%self.mod
                        a[i+offset]=(a0+a1+a2+a3)%self.mod
                        a[i+offset+p]=(a0-a1+a2na3iimag)*irot%self.mod
                        a[i+offset+2*p]=(a0+a1-a2-a3)*irot2%self.mod
                        a[i+offset+3*p]=(a0-a1-a2na3iimag)*irot3%self.mod
                    irot*=self.irate3[(~s & -~s).bit_length()-1]
                    irot%=self.mod
                LEN-=2
    def convolution(self,a,b):
        n=len(a);m=len(b)
        if not(a) or not(b):
            return []
        if min(n,m)<=40:
            res=[0]*(n+m-1)
            for i in range(n):
                for j in range(m):
                    res[i+j]+=a[i]*b[j]
                    res[i+j]%=self.mod
            return res
        z=1<<((n+m-2).bit_length())
        a=a+[0]*(z-n)
        b=b+[0]*(z-m)
        self.butterfly(a)
        self.butterfly(b)
        c=[(a[i]*b[i])%self.mod for i in range(z)]
        self.butterfly_inv(c)
        iz=pow(z,self.mod-2,self.mod)
        for i in range(n+m-1):
            c[i]=(c[i]*iz)%self.mod
        return c[:n+m-1]
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
n,m,k = map(int,input().split())
s = input()
t = input()
mod = pow(2,61)-1
h = [0 for i in range(n)]
for i in range(n-m+1):
    if i == 0:
        for j in range(m):
            if ord('A') <= ord(s[j]) <= ord('Z'):
                h[0] += (ord(s[j])-ord('A')) * pow(26,j,mod)
                h[0] %= mod
            else:
                h[0] += (ord(s[j])-ord('a')) * pow(26,j,mod)
                h[0] %= mod
    else:
        h[i] = h[i-1] - (ord(s[i-1])-ord('A') if ord('A') <= ord(s[i-1]) <= ord('Z') else ord(s[i-1])-ord('a'))
        h[i] *= pow(26,mod-2,mod)
        h[i] += pow(26,m-1,mod) * (ord(s[i+m-1])-ord('A') if ord('A') <= ord(s[i+m-1]) <= ord('Z') else ord(s[i+m-1])-ord('a'))
        h[i] %= mod
tval = 0
for j in range(m):
    if ord('A') <= ord(t[j]) <= ord('Z'):
        tval += (ord(t[j])-ord('A')) * pow(26,j,mod)
        tval %= mod
    else:
        tval += (ord(t[j])-ord('a')) * pow(26,j,mod)
        tval %= mod
ss = [-1 if ord('A') <= ord(s[j]) <= ord('Z') else 1 for j in range(n)]
tt = [-1 if ord('A') <= ord(t[j]) <= ord('Z') else 1 for j in range(m)]

tt.reverse()
conv = FFT(998244353)
st = conv.convolution(ss,tt)
ans = 0
for i in range(n-m+1):
    if h[i] == tval:
        x = st[m-1+i]
        #print(i,x)
        if m-2*k <= x <= m-2 or m-2*k <= x-998244353 <= m-2:
            ans += 1
print(ans)
0