QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353496 | #5312. Levenshtein Distance | Graphcity | TL | 13ms | 45276kb | C++20 | 2.9kb | 2024-03-14 10:09:53 | 2024-03-14 10:09:53 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=1e6,Maxk=1e5;
int n,m,k; char s[Maxn+5],t[Maxn+5];
int rk[Maxn+5],sa[Maxn+5],h[Maxn+5],st[Maxn+5][20];
int ans[Maxn+5],f[Maxk+5][64],mn[Maxn+5];
struct SA
{
int n,i,t,p,m,id[Maxn+5],num[Maxn+5],old[Maxn+5],cnt[Maxn+5]; char s[Maxn+5];
inline int cmp(int x,int y) {return (old[x]==old[y] && old[x+t]==old[y+t]);}
inline void Run()
{
m=300; For(i,1,m) cnt[i]=0;
for(i=1;i<=n;++i) cnt[rk[i]=s[i]]++;
for(i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for(i=n;i>=1;--i) sa[cnt[rk[i]]--]=i;
for(t=1;t<=n;t<<=1,m=p)
{
for(p=0,i=n;i>n-t;--i) id[++p]=i;
for(i=1;i<=n;++i) if(sa[i]>t) id[++p]=sa[i]-t;
For(i,1,m) cnt[i]=0;
for(i=1;i<=n;++i) cnt[num[i]=rk[id[i]]]++;
for(i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for(i=n;i>=1;--i) sa[cnt[num[i]]--]=id[i];
For(i,1,n) old[i]=rk[i];
for(p=0,i=1;i<=n;++i) rk[sa[i]]=cmp(sa[i],sa[i-1])?p:++p;
if(p==n) break;
}
for(int i=1;i<=n;++i) rk[sa[i]]=i,h[i]=0;
for(int i=1,it=0;i<=n;++i)
{
if(rk[i]==0) continue; if(it) --it;
while(s[i+it]==s[sa[rk[i]-1]+it]) ++it;
h[rk[i]]=it;
}
For(i,1,n) st[i][0]=h[i];
For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
} S;
inline int LCP(int x,int y)
{
if(x>n || y>m) return 0;
int l=rk[x],r=rk[y+n+1]; if(l>r) swap(l,r);
int len=__lg(r-l++); return min(st[l][len],st[r-(1<<len)+1][len]);
}
inline int& F(int x,int y) {return f[x][y+k];}
inline void Init()
{
scanf("%d%s%s",&k,s+1,t+1),n=strlen(s+1),m=strlen(t+1);
s[n+1]='#',t[m+1]='$'; S.n=n+m+2;
For(i,1,n+1) S.s[i]=s[i]; For(i,1,m+1) S.s[n+1+i]=t[i]; S.Run();
}
int main()
{
// freopen("1.in","r",stdin);
Init();
For(st,0,m-1)
{
memset(f,-1,sizeof(f)),F(0,0)=0;
For(i,0,k) For(j,-i,i) if(F(i,j)!=-1)
{
F(i,j)+=LCP(F(i,j)+1,st+F(i,j)+j+1);
if(i<k)
{
F(i+1,j+1)=max(F(i+1,j+1),F(i,j));
F(i+1,j)=max(F(i+1,j),F(i,j)+1);
F(i+1,j-1)=max(F(i+1,j-1),F(i,j));
}
}
int L=max(st+1,st+n-k),R=min(m,st+n+k); if(L>R) continue;
For(i,L,R) mn[i]=k+1;
For(i,0,k) For(j,-i,i) if(F(i,j)>=n)
mn[min(m,st+n+j)]=min(mn[min(m,st+n+j)],i);
For(i,L+1,R) mn[i]=min(mn[i],mn[i-1]+1);
Rof(i,R-1,L) mn[i]=min(mn[i],mn[i+1]+1);
For(i,L,R) if(mn[i]<=k) ans[mn[i]]++;
// if(st==0) cerr<<F(2,-2)<<endl;
// For(i,L,R) cerr<<st<<' '<<i<<' '<<mn[i]<<endl;
}
For(i,0,k) printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 13ms
memory: 45276kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: -100
Time Limit Exceeded
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...