結果
問題 | No.2626 Similar But Different Name |
ユーザー | PNJ |
提出日時 | 2024-02-09 23:08:39 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 11,028 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 82,320 KB |
実行使用メモリ | 289,160 KB |
最終ジャッジ日時 | 2024-09-28 16:21:07 |
合計ジャッジ時間 | 36,719 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
55,908 KB |
testcase_01 | AC | 39 ms
55,244 KB |
testcase_02 | AC | 38 ms
55,528 KB |
testcase_03 | AC | 38 ms
56,068 KB |
testcase_04 | AC | 38 ms
56,732 KB |
testcase_05 | AC | 43 ms
62,252 KB |
testcase_06 | AC | 43 ms
63,136 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 107 ms
77,500 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
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 | - |
ソースコード
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] class string: def sa_is(s,upper): n=len(s) if n==0: return [] if n==1: return [0] if n==2: if (s[0]<s[1]): return [0,1] else: return [1,0] sa=[0]*n ls=[0]*n for i in range(n-2,-1,-1): ls[i]=ls[i+1] if (s[i]==s[i+1]) else (s[i]<s[i+1]) sum_l=[0]*(upper+1) sum_s=[0]*(upper+1) for i in range(n): if not(ls[i]): sum_s[s[i]]+=1 else: sum_l[s[i]+1]+=1 for i in range(upper+1): sum_s[i]+=sum_l[i] if i<upper: sum_l[i+1]+=sum_s[i] def induce(lms): for i in range(n): sa[i]=-1 buf=sum_s[:] for d in lms: if d==n: continue sa[buf[s[d]]]=d buf[s[d]]+=1 buf=sum_l[:] sa[buf[s[n-1]]]=n-1 buf[s[n-1]]+=1 for i in range(n): v=sa[i] if v>=1 and not(ls[v-1]): sa[buf[s[v-1]]]=v-1 buf[s[v-1]]+=1 buf=sum_l[:] for i in range(n-1,-1,-1): v=sa[i] if v>=1 and ls[v-1]: buf[s[v-1]+1]-=1 sa[buf[s[v-1]+1]]=v-1 lms_map=[-1]*(n+1) m=0 for i in range(1,n): if not(ls[i-1]) and ls[i]: lms_map[i]=m m+=1 lms=[] for i in range(1,n): if not(ls[i-1]) and ls[i]: lms.append(i) induce(lms) if m: sorted_lms=[] for v in sa: if lms_map[v]!=-1: sorted_lms.append(v) rec_s=[0]*m rec_upper=0 rec_s[lms_map[sorted_lms[0]]]=0 for i in range(1,m): l=sorted_lms[i-1] r=sorted_lms[i] end_l=lms[lms_map[l]+1] if (lms_map[l]+1<m) else n end_r=lms[lms_map[r]+1] if (lms_map[r]+1<m) else n same=True if end_l-l!=end_r-r: same=False else: while(l<end_l): if s[l]!=s[r]: break l+=1 r+=1 if (l==n) or (s[l]!=s[r]): same=False if not(same): rec_upper+=1 rec_s[lms_map[sorted_lms[i]]]=rec_upper rec_sa=string.sa_is(rec_s,rec_upper) for i in range(m): sorted_lms[i]=lms[rec_sa[i]] induce(sorted_lms) return sa def suffix_array_upper(s,upper): assert 0<=upper for d in s: assert 0<=d and d<=upper return string.sa_is(s,upper) def suffix_array(s): n=len(s) if type(s)==str: s2=[ord(i) for i in s] return string.sa_is(s2,255) else: idx=list(range(n)) idx.sort(key=lambda x:s[x]) s2=[0]*n now=0 for i in range(n): if (i& s[idx[i-1]]!=s[idx[i]]): now+=1 s2[idx[i]]=now return string.sa_is(s2,now) def lcp_array(s,sa): n=len(s) assert n>=1 rnk=[0]*n for i in range(n): rnk[sa[i]]=i lcp=[0]*(n-1) h=0 for i in range(n): if h>0: h-=1 if rnk[i]==0: continue j=sa[rnk[i]-1] while(j+h<n and i+h<n): if s[j+h]!=s[i+h]: break h+=1 lcp[rnk[i]-1]=h return lcp N,M,K = map(int,input().split()) P,PP = [0 for i in range(N)],[0 for i in range(N)] Q,QQ = [0 for i in range(M)],[0 for i in range(M)] S = input() S = [S[i] for i in range(N)] for i in range(N): if ord(S[i]) < 97: P[i] = 1 S[i] = chr(ord(S[i]) + 32) else: PP[i] = 1 T = input() T = [T[i] for i in range(M)] for i in range(M): if ord(T[i]) < 97: QQ[i] = 1 T[i] = chr(ord(T[i]) + 32) else: Q[i] = 1 P = P[::-1] PP = PP[::-1] R = ''.join(S + T) SA = string.suffix_array(R) LCA = string.lcp_array(R,SA) l = 0 while SA[l] != N: l += 1 ans = [0 for i in range(N)] m = M for j in range(l-1,-1,-1): m = min(m,LCA[j]) if m == M: ans[SA[j]] = 1 else: break for j in range(l,len(R) - 1): m = min(m,LCA[j]) if m == M: ans[SA[j+1]] = 1 else: break CONV = FFT(998244353) F = CONV.convolution(P,Q) G = CONV.convolution(PP,QQ) for i in range(N): n = N - 1 - i if F[n] + G[n] > K: ans[i] = 0 elif F[n] + G[n] == 0: ans[i] = 0 print(sum(ans))