QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#75799 | #5312. Levenshtein Distance | A_zjzj | WA | 23ms | 29480kb | C++14 | 2.3kb | 2023-02-06 11:29:28 | 2023-02-06 11:29:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+10,M=N*2,K=61,P=__lg(M)+2;
int n,m,k,ans[K],mn[N];
char a[N],b[N],c[M];
int tot,cnt[M],rk[M],sa[M],old[M<<1],p[M],id[M],h[P][M];
struct ary{
int a[K];
int& operator [] (const int &x){
return a[x+k];
}
const int& operator [] (const int &x)const{
return a[x+k];
}
}f[N];
void getsa(char *a,int n,int m=127){
for(int i=1;i<=n;i++)cnt[rk[i]=a[i]]++;
for(int i=1;i<=m;i++)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--)sa[cnt[rk[i]]--]=i;
fill(cnt,cnt+1+m,0);
for(int len=1,t;len==1||m^n;len<<=1,m=t){
t=0;for(int i=n-len+1;i<=n;i++)p[++t]=i;
for(int i=1;i<=n;i++)if(sa[i]>len)p[++t]=sa[i]-len;
for(int i=1;i<=n;i++)cnt[id[i]=rk[p[i]]]++,old[i]=rk[i];
for(int i=1;i<=m;i++)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--)sa[cnt[id[i]]--]=p[i];
fill(cnt,cnt+1+m,0);
t=0;for(int i=1;i<=n;i++)rk[sa[i]]=old[sa[i]]==old[sa[i-1]]&&old[sa[i]+len]==old[sa[i-1]+len]?t:++t;
}
for(int i=1,x=0;i<=n;i++){
if(x)x--;
for(;max(i,sa[rk[i]-1])+x<=n&&a[i+x]==a[sa[rk[i]-1]+x];x++);
h[0][rk[i]]=x;
}
for(int i=1;(1<<i)<=n;i++)for(int j=1;j+(1<<i)-1<=n;j++)h[i][j]=min(h[i-1][j],h[i-1][j+(1<<i-1)]);
}
int LCP(int l,int r){
l=rk[l],r=rk[r];
if(l>r)swap(l,r);
int k=__lg(r-l++);
return min(h[k][l],h[k][r-(1<<k)+1]);
}
int main(){
scanf("%d%s%s",&k,a+1,b+1);
n=strlen(a+1),m=strlen(b+1);
for(int i=1;i<=n;i++)c[++tot]=a[i];
c[++tot]='#';
for(int i=1;i<=m;i++)c[++tot]=b[i];
c[++tot]='$';
getsa(c,tot);
if(tot>20)return 0;
for(int st=0;st<m;st++){
memset(f,-1,sizeof f);
f[0][0]=LCP(1,1+st+1+n)+1;
for(int i=0;i<k;i++){
for(int j=-i;j<=i;j++)if(~f[i][j]){
int x=f[i][j];
if(x<=n&&x+j+st<=m)f[i+1][j]=max(f[i+1][j],x+1+LCP(x+1,x+1+j+st+1+n));
if(x+j+st<=m)f[i+1][j+1]=max(f[i+1][j+1],x+LCP(x,x+1+j+st+1+n));
if(x<=n)f[i+1][j-1]=max(f[i+1][j-1],x+1+LCP(x+1,x+j+st+1+n));
}
}
int L=max(st+n-k-k,st+1),R=min(st+n+k+k,m);
for(int i=L;i<=R;i++)mn[i]=k+1;
for(int j=-k;j<=k;j++){
for(int x=0;x<=k;x++){
if(f[x][j]==n+1){
mn[st+n+j]=min(mn[st+n+j],x);
}
}
}
for(int i=L;i<R;i++)mn[i+1]=min(mn[i+1],mn[i]+1);
for(int i=R;i>L;i--)mn[i-1]=min(mn[i-1],mn[i]+1);
for(int i=L;i<=R;i++)ans[mn[i]]++;
}
for(int i=0;i<=k;i++)printf("%d\n",ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 23ms
memory: 29480kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: -100
Wrong Answer
time: 9ms
memory: 14020kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
result:
wrong answer Answer contains longer sequence [length = 31], but output contains 0 elements