QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865648#5312. Levenshtein DistanceQing_HyunRE 2ms8692kbC++143.3kb2025-01-21 20:45:392025-01-21 20:45:44

Judging History

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

  • [2025-01-21 20:45:44]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:8692kb
  • [2025-01-21 20:45:39]
  • 提交

answer

#include <bits/stdc++.h>
#define fo(i, a, b) for(register int i = (a); (i - 1) - (b) >> 31; ++ i)
#define fd(i, a, b) for(register int i = (a); ((b) - 1) - i >> 31; -- i)
#define ll long long
//#define int long long

using namespace std;

const int N = 1e5 + 5, Mod = 1e9 + 7;

int n, m, sa[N], rak[N], k;
char s[N], t[N];
int St[N][24], Min[N];
int f[36][72];

//aaa=aabbaab@
/*
4
aaa
aabbaab
4 12 3 2 1 9 5 10 6 11 8 7
*/
struct SA {
#define lst tp
	int n, m;
	char s[N];
	int tp[N], tot[N], h[N];
	void Qsort() {
		fo(i, 0, m)tot[i] = 0;
		fo(i, 1, n)tot[rak[i]] ++;
		fo(i, 1, m)tot[i] += tot[i - 1];
		fd(i, n, 1)sa[tot[rak[tp[i]]] --] = tp[i];
	}
	void Get_sa() {
		m = 300;
		fo(i, 1, n)
		rak[i] = s[i], tp[i] = i;
		Qsort();
//		return;
		for(int w = 1, cnt = 0; cnt < n; w <<= 1, m = cnt) {
			cnt = 0;
			fo(i, 1, w)tp[++ cnt] = n - w + i;
			fo(i, 1, n)if(sa[i] > w)tp[++ cnt] = sa[i] - w;
			Qsort();
			swap(tp, rak);
			rak[sa[1]] = cnt = 1;
			fo(i, 2, n)rak[sa[i]] = lst[sa[i - 1]] == lst[sa[i]] && lst[sa[i - 1] + w] == lst[sa[i] + w] ? cnt : ++ cnt;
		}
		return;
	}
	void Get_Height() {
		int k = 0;
		fo(i, 1, n) {
			if(rak[i] == 1)
				continue;
			k = k ? k - 1 : k;
			for(int j = sa[rak[i] - 1]; i + k <= n && j + k <= n && s[i + k] == s[j + k]; k ++);
			h[rak[i]] = k;
		}
//		fo(i, 1, n)
//			cout << h[i] << " ";
//		cout << "\n";
		return;
	}
	void Get_LCP() {
		fo(i, 1, n)
		St[i][0] = h[i];
		fo(c, 1, int(log2(n)))
		fo(i, 1, n)
		St[i][c] = min(St[i][c - 1], St[i + (1 << (c - 1))][c - 1]);
	}

	void init() {
		Get_sa();
		Get_Height();
		Get_LCP();
	}
} S;

int LCP(int x, int y) {
	if(x > n || y > m)
		return 0;
	int l = rak[x], r = rak[y + n + 1];
	if(l > r)
		swap(l, r);
	int k = __lg(r - l ++);
	return min(St[l][k], St[r - (1 << k) + 1][k]);
}

void init() {
	cin >> k >> s + 1 >> t + 1;
	n = strlen(s + 1), m = strlen(t + 1);
	s[n + 1] = '=', t[m + 1] = '@';
	fo(i, 1, n + 1)
	S.s[i] = s[i];
	fo(i, 1, m + 1)
	S.s[i + n + 1] = t[i];
	S.n = n + m + 2;
//	fo(i, 1, S.n)
//		cout << S.s[i];
	S.init();
	return;
}

int ans[36];
inline int& dp(int x, int y) {return f[x][y + k];}

void solve() {
	init();
	fo(st, 0, m - 1) {
		memset(f, -1, sizeof f);
		dp(0, 0) = 0;
		fo(i, 0, k)
		fo(j, -i, i) {
			if(dp(i, j) == -1)
				continue;
//			cout << LCP(dp(i, j) + 1, st + dp(i, j) + j + 1) << "! ";
			dp(i, j) += LCP(dp(i, j) + 1, st + dp(i, j) + j + 1);
//			cout << dp(i, j) << "\n";
			if(i < k) {
				dp(i + 1, j - 1) = max(dp(i + 1, j - 1), dp(i, j) + 1);
				dp(i + 1, j) = max(dp(i + 1, j), dp(i, j) + 1);
				dp(i + 1, j + 1) = max(dp(i + 1, j + 1), dp(i, j));
			}
//			cout << i << " " << j << " " << dp(i, j) << "\n";
//			cout << dp(i + 1, j) << "\n";
		}
//		continue;
		int L = max(st + 1, st + n - k), R = min(m, st + n + k);
		if(L > R) continue;
		fo(i, L, R) Min[i] = k + 1;
		fo(i, 0, k)
		fo(j, -i, i)
		if(dp(i, j) >= n)
			Min[min(m, min(m, st + n + j))] = min(Min[min(m, st + n + j)], i);
		fo(i, L, R) {
//			cout << Min[i] << " " << k << "\n";
			if(Min[i] <= k)
				ans[Min[i]] ++;
		}
	}
	fo(i, 0, k)
		cout << ans[i] << "\n";
	return;
}

signed main() {

//	ios::sync_with_stdio(0);
//	cin.tie(0), cout.tie(0);

	int T = 1;
//	cin >> T;

	while(T --) {
		solve ();
	}

	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8692kb

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: