QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#75797#5312. Levenshtein DistanceA_zjzjRE 18ms29476kbC++142.3kb2023-02-06 11:27:122023-02-06 11:28:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-06 11:28:10]
  • 评测
  • 测评结果:RE
  • 用时:18ms
  • 内存:29476kb
  • [2023-02-06 11:27:12]
  • 提交

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];
		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);
	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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 18ms
memory: 29476kb

input:

4
aaa
aabbaab

output:

0
5
15
7
1

result:

ok 5 number(s): "0 5 15 7 1"

Test #2:

score: -100
Runtime Error

input:

30
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

output:


result: