QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#739558#5312. Levenshtein DistancePatrickStarTL 0ms8900kbC++141.6kb2024-11-12 22:10:282024-11-12 22:10:32

Judging History

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

  • [2024-11-12 22:10:32]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:8900kb
  • [2024-11-12 22:10:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 2e5, base = 31, mod = 1e9+7;
int K, n, m, ans[35];
char a[MAXN+5], b[MAXN+5];
int dp[35][65];
ll H1[MAXN+5], H2[MAXN+5], pw[MAXN+5];
ll get1(int l, int r) {
	return (H1[r]-H1[l-1]*pw[r-l+1]%mod+mod)%mod;
}
ll get2(int l, int r) {
	return (H2[r]-H2[l-1]*pw[r-l+1]%mod+mod)%mod;
}
int main() {
	scanf("%d", &K);
	scanf("%s", a+1);
	n = strlen(a+1);
	scanf("%s", b+1);
	m = strlen(b+1);
	pw[0] = 1;
	for (int p=1;p<=MAXN;p++) {
		pw[p] = pw[p-1]*base%mod;
	}
	for (int p=1;p<=n;p++) {
		H1[p] = (H1[p-1]*base+a[p])%mod;
	}
	for (int p=1;p<=m;p++) {
		H2[p] = (H2[p-1]*base+b[p])%mod;
	}
	for (int p=1;p<=m;p++) {
		memset(dp, -0x3f, sizeof(dp));
		dp[0][0+K] = 0;
		for (int i=0;i<=K;i++) {
			for (int j=-K;j<=K;j++) {
				if (dp[i][j+K]>=0&&dp[i][j+K]+p-1+j<=m&&dp[i][j+K]+p-1+j>=p-1) {
					int p1 = dp[i][j+K], p2 = dp[i][j+K]+p-1+j;
					int L = 0, R = min(n-p1, m-p2);
					while (L<R) {
						int mid = (L+R+1)/2;
						if (get1(p1+1, p1+mid)==get2(p2+1, p2+mid)) L = mid;
						else R = mid-1;
					}
					dp[i][j+K]+=L;
					dp[i+1][j+K+1] = max(dp[i+1][j+K+1], dp[i][j+K]);
					if (j!=-K) dp[i+1][j+K-1] = max(dp[i+1][j+K-1], min(n, dp[i][j+K]+1));
					if (dp[i][j+K]!=n) dp[i+1][j+K] = max(dp[i+1][j+K], dp[i][j+K]+1);
				}
			}
		}
		for (int j=-K;j<=K;j++) {
			for (int i=0;i<=K;i++) {
				if (dp[i][j+K]==n&&dp[i][j+K]+p-1+j<=m&&dp[i][j+K]+p-1+j>=p) {
					ans[i]++;
					break;
				}
			}
		}
	}
	for (int p=0;p<=K;p++) {
		printf("%d\n", ans[p]);
	}
	return 0;
}

详细

Test #1:

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

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...

output:


result: