QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403675#5312. Levenshtein DistanceqwqUwU_WA 677ms22824kbC++172.6kb2024-05-02 17:11:192024-05-02 17:11:19

Judging History

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

  • [2024-05-02 17:11:19]
  • 评测
  • 测评结果:WA
  • 用时:677ms
  • 内存:22824kb
  • [2024-05-02 17:11:19]
  • 提交

answer

// clang-format off
#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll; 
typedef unsigned long long ull; 
typedef pair<int,int> pii; typedef pair<ll,int> pli; typedef pair<ll,ll> pll; typedef pair<int,ll> pil;
inline ll read(){
	ll x=0,f=1,c=getchar();
	while(c<'0'||c>'9')f=(c=='-'?-1:1),c=getchar();
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
const int N=1e5+3;
// clang-format on
int n,m,K;
char S[N],T[N];
int A[N<<1];
int sa[N<<1],rk[N<<1],st[18][N<<1];
inline int lcp(int i,int j){
	if(i>n||j>m)return 0;
	i=rk[i],j=rk[j+n+1];
	if(i>j)swap(i,j);
	int k=__lg(j-i++);
	return min(st[k][i],st[k][j-(1<<k)+1]);
}
int main() {
    //freopen("data.in", "r", stdin);
    // freopen(".in","r",stdin);
    // freopen("myans.out","w",stdout);
	K=read();scanf("%s",S+1),n=strlen(S+1);
	scanf("%s",T+1);m=strlen(T+1);
	rep(i,1,n)A[i]=S[i];A[n+1]=32;
	rep(i,1,m)A[n+1+i]=T[i];
	static int cnt[N<<1];int len=n+m+1,lim=127;
	rep(i,1,len)++cnt[rk[i]=A[i]];
	rep(i,1,lim)cnt[i] += cnt[i-1];
	per(i,len,1)sa[cnt[rk[i]]--]=i;
	for(int w=1,p=0;w<=len;w<<=1,lim=p,p=0){
		static int id[N<<1];
		per(i,len,len-w+1)id[++p]=i;
		rep(i,1,len)if(sa[i]>w)id[++p]=sa[i]-w;
		memset(cnt,0,sizeof cnt);
		rep(i,1,len)++cnt[rk[id[i]]];
		rep(i,1,lim)cnt[i] += cnt[i-1];
		per(i,len,1)sa[cnt[rk[id[i]]]--]=id[i],id[i]=0;
		static int tmp[N<<1];swap(tmp,rk);
		p=0;rep(i,1,len)rk[sa[i]]=(tmp[sa[i]]==tmp[sa[i-1]]&&tmp[sa[i]+w]==tmp[sa[i-1]+w]?p:++p);
		if(p==n)break;
	}
	rep(i,1,len){
		if(rk[i]==1)continue;
		int &k=st[0][rk[i]]=max(0,st[0][rk[i-1]]-1);
		while(A[i+k]==A[sa[rk[i]-1]+k])++k;
	}
	rep(j,1,17)rep(i,1,len-(1<<j)+1)st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
	static int f[33][63],ans[33];
	rep(k,0,m-1){
		memset(f,0xfe,sizeof f);
		f[0][K]=lcp(1,k+1);
		//rep(i,1,min(K,n))f[i][K-i]=lcp(i+1,k+1)+i;
		//rep(i,1,min(K,m-k))f[i][K+i]=lcp(1,k+i+1);
		rep(v,0,K-1)rep(d,-min(K,n-1),min(K,m-k-1)){
			int i=f[v][d+K],j=i+d;
			if(i<0||j<0)continue;
			if(j<m-k)f[v+1][d+K+1]=max(f[v+1][d+K+1],f[v][d+K]+lcp(i+1,k+j+2));
			if(i<n)f[v+1][d+K-1]=max(f[v+1][d+K-1],f[v][d+K]+1+lcp(i+2,k+j+1));
			if(i<n&&j<m-k)f[v+1][d+K]=max(f[v+1][d+K],f[v][d+K]+1+lcp(i+2,k+j+2));
		}
		rep(d,-min(K,n-1),min(K,m-n-k)) rep(v,0,K)if(f[v][d+K]>=n){++ans[v];break;}
		int lim=f[0][K];
		rep(i,max(0,n-lim+1),min(K,n-1))++ans[i];
	}
	rep(i,0,K)printf("%d\n",ans[i]);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 11256kb

input:

4
aaa
aabbaab

output:

0
5
15
7
1

result:

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

Test #2:

score: 0
Accepted
time: 677ms
memory: 21844kb

input:

30
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 31 numbers

Test #3:

score: -100
Wrong Answer
time: 271ms
memory: 22824kb

input:

30
BBAAABAABBBA
ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...

output:

17
662
7733
45232
144312
265981
305388
240835
163242
128831
124386
131128
105164
102167
99782
97735
96062
94732
93635
92721
92060
91525
91143
90831
90585
90419
90288
90200
90120
90068
90035

result:

wrong answer 3rd numbers differ - expected: '8230', found: '7733'