結果
問題 | No.2626 Similar But Different Name |
ユーザー | nikoro256 |
提出日時 | 2024-02-13 14:12:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,335 ms / 3,000 ms |
コード長 | 9,892 bytes |
コンパイル時間 | 147 ms |
コンパイル使用メモリ | 82,548 KB |
実行使用メモリ | 256,832 KB |
最終ジャッジ日時 | 2024-09-28 18:23:19 |
合計ジャッジ時間 | 22,759 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
56,444 KB |
testcase_01 | AC | 42 ms
57,752 KB |
testcase_02 | AC | 43 ms
57,060 KB |
testcase_03 | AC | 43 ms
58,116 KB |
testcase_04 | AC | 42 ms
56,624 KB |
testcase_05 | AC | 44 ms
58,300 KB |
testcase_06 | AC | 41 ms
57,264 KB |
testcase_07 | AC | 71 ms
75,316 KB |
testcase_08 | AC | 69 ms
74,356 KB |
testcase_09 | AC | 95 ms
77,444 KB |
testcase_10 | AC | 101 ms
78,352 KB |
testcase_11 | AC | 99 ms
78,508 KB |
testcase_12 | AC | 95 ms
78,072 KB |
testcase_13 | AC | 98 ms
78,220 KB |
testcase_14 | AC | 122 ms
78,700 KB |
testcase_15 | AC | 125 ms
78,828 KB |
testcase_16 | AC | 121 ms
78,832 KB |
testcase_17 | AC | 123 ms
78,820 KB |
testcase_18 | AC | 1,335 ms
250,988 KB |
testcase_19 | AC | 278 ms
159,468 KB |
testcase_20 | AC | 255 ms
158,780 KB |
testcase_21 | AC | 554 ms
216,652 KB |
testcase_22 | AC | 1,029 ms
186,124 KB |
testcase_23 | AC | 1,021 ms
186,692 KB |
testcase_24 | AC | 1,004 ms
182,352 KB |
testcase_25 | AC | 1,036 ms
185,452 KB |
testcase_26 | AC | 1,031 ms
186,984 KB |
testcase_27 | AC | 1,036 ms
187,376 KB |
testcase_28 | AC | 1,025 ms
183,968 KB |
testcase_29 | AC | 1,163 ms
213,644 KB |
testcase_30 | AC | 1,131 ms
213,680 KB |
testcase_31 | AC | 1,150 ms
207,252 KB |
testcase_32 | AC | 1,164 ms
208,304 KB |
testcase_33 | AC | 1,161 ms
212,984 KB |
testcase_34 | AC | 1,125 ms
213,648 KB |
testcase_35 | AC | 1,124 ms
215,164 KB |
testcase_36 | AC | 1,114 ms
213,720 KB |
testcase_37 | AC | 1,330 ms
256,832 KB |
ソースコード
from collections import Counter # SA-IS (O(nlogn)) # s: 文字列 def sais(s): uniq = list(set(s)) uniq.sort() return sais_rec(list(map({e: i+1 for i, e in enumerate(uniq)}.__getitem__, s)), len(uniq)) def sais_rec(lst, num): L = len(lst) if L < 2: return lst + [0] lst = lst + [0] L += 1 res = [-1] * L t = [1] * L for i in range(L-2, -1, -1): t[i] = 1 if (lst[i] < lst[i+1] or (lst[i] == lst[i+1] and t[i+1])) else 0 isLMS = [t[i-1] < t[i] for i in range(L)] LMS = [i for i in range(1, L) if t[i-1] < t[i]] LMSn = len(LMS) count = Counter(lst) tmp = 0 cstart = [0]*(num+1) cend = [0]*(num+1) for key in range(num+1): cstart[key] = tmp cend[key] = tmp = tmp + count[key] cc_it = [iter(range(e-1, s-1, -1)) for s, e in zip(cstart, cend)] for e in reversed(LMS): res[next(cc_it[lst[e]])] = e cs_it = [iter(range(s, e)) for s, e in zip(cstart, cend)] ce_it = [iter(range(e-1, s-1, -1)) for s, e in zip(cstart, cend)] for e in res: if e > 0 and not t[e-1]: res[next(cs_it[lst[e-1]])] = e-1 for e in reversed(res): if e > 0 and t[e-1]: res[next(ce_it[lst[e-1]])] = e-1 name = 0; prev = -1 pLMS = {} for e in res: if isLMS[e]: if prev == -1 or lst[e] != lst[prev]: name += 1; prev = e else: for i in range(1, L): if lst[e+i] != lst[prev+i]: name += 1; prev = e break if isLMS[e+i] or isLMS[prev+i]: break pLMS[e] = name-1 if name < LMSn: sublst = [pLMS[e] for e in LMS if e < L-1] ret = sais_rec(sublst, name-1) LMS = list(map(LMS.__getitem__, reversed(ret))) else: LMS = [e for e in reversed(res) if isLMS[e]] res = [-1] * L cc_it = [iter(range(e-1, s-1, -1)) for s, e in zip(cstart, cend)] for e in LMS: res[next(cc_it[lst[e]])] = e cs_it = [iter(range(s, e)) for s, e in zip(cstart, cend)] ce_it = [iter(range(e-1, s-1, -1)) for s, e in zip(cstart, cend)] for e in res: if e > 0 and not t[e-1]: res[next(cs_it[lst[e-1]])] = e-1 for e in reversed(res): if e > 0 and t[e-1]: res[next(ce_it[lst[e-1]])] = e-1 return res # Longest Common Prefix # (文字列s, 文字列長n, Suffix Array)を引数として与える def LCP(s, n, sa): lcp = [-1]*(n+1) rank = [0]*(n+1) for i in range(n+1): rank[sa[i]] = i h = 0 lcp[0] = 0 for i in range(n): j = sa[rank[i] - 1] if h > 0: h -= 1 while j+h < n and i+h < n and s[j+h]==s[i+h]: h += 1 lcp[rank[i] - 1] = h return lcp #https://github.com/shakayami/ACL-for-python/blob/master/convolution.py 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] def nibun(f): left,right=-1,N+1 while right-left>1: mid=(right+left)//2 if f(mid): left=mid else: right=mid return right def isok(n): pos=sa[n] if S[pos:].upper()<T.upper(): return True return False def isok2(n): pos=sa[n] if S[pos:].upper()<T.upper()+'z': return True return False N,M,K=map(int,input().split()) S=input() T=input() sa=sais(S.upper()) one=nibun(isok) two=nibun(isok2) s=[] t=[] for si in S: if si.isupper(): s.append(1) else: s.append(-1) for si in T: if si.isupper(): t.append(1) else: t.append(-1) fft=FFT(998244353) fa=fft.convolution(s,t[::-1]) ans=0 for i in range(one,two): tmp=fa[sa[i]+M-1] if tmp>=10**8: tmp-=998244353 if M>tmp>=M-K*2: ans+=1 print(ans)