QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196657#7234. Pencil of WishingjeffqiWA 1ms3860kbC++202.7kb2023-10-01 21:09:482023-10-01 21:09:48

Judging History

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

  • [2023-10-01 21:09:48]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3860kb
  • [2023-10-01 21:09:48]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define ui unsigned int
#define ull unsigned ll
#define i128 __int128
using namespace std;

namespace qiqi {
	const int inf = 1e9;
	void main() {
		string s,t;
		cin >> s >> t;
		int n = sz(s),m = sz(t);
		vector<vi> lcp(n+1,vi(m+1));
		for (int i = n-1; i >= 0; i--) {
			for (int j = m-1; j >= 0; j--) {
				lcp[i][j] = s[i] == t[j] ? lcp[i+1][j+1]+1 : 0;
			}
		}
		int ans = min(n,lcp[0][0]+2); pii pans({});
		vector<vi> f(n+1,vi(m+1,inf));
		vector<vector<pii>> g(n+1,vector<pii>(m+1));
		for (int i = 0; i <= lcp[0][0]; i++) {
			f[i][i] = i+1; g[i][i] = pii(0,0);
		}
		for (int i = 1; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (f[i][j] > f[i-1][j]) {
					f[i][j] = f[i-1][j];
					g[i][j] = pii(i-1,j);
				}
			}
			for (int j = 1; j < n-i; j++) {
				int lst = -1;
				for (int k = 0; k <= m-j; k++) {
					if (lcp[i][k] >= j) {
						for (int o = lst+1; o <= k; o++) {
							if (f[i+j][k+j] > f[i][o]+j+1) {
								f[i+j][k+j] = f[i][o]+j+1;
								g[i+j][k+j] = pii(i,o);
							}
						}
						lst = k;
					}
				}
				for (int o = lst+1; o <= m; o++) {
					if (ans > f[i][o]+j+1) {
						ans = f[i][o]+j+1;
						pans = pii(i,o);
					}
				}
			}
			int len = n-i;
			for (int j = lcp[i][m-len] != len ? 0 : m-len+1; j <= m; j++) {
				if (ans > f[i][j]+len) {
					ans = f[i][j]+len;
					pans = pii(i,j);
				}
			}
		}
		if (ans == n) {
			cout << s << '\n';
			return;
		}
		if (ans == lcp[0][0]+2) {
			cout << string(s.begin(),s.begin()+lcp[0][0]+1)+"*" << '\n';
			return;
		}
		vector<string> vec;
		auto [p,q] = pans;
		int k = ans;
		if (p != n) {
			vec.eb("*");
			k--;
		}
		while (k) {
			vec.eb(string(s.begin()+p,s.begin()+p+k-f[p][q]));
			k = f[p][q];
			if (p) {
				vec.eb("*");
				k--;
			}
			while (p && g[p][q].se == q) {
				tie(p,q) = g[p][q];
			}
			tie(p,q) = g[p][q];
		}
		reverse(all(vec));
		string sans;
		for (const auto &str:vec) {
			sans += str;
		}
		cout << sans << '\n';
	}
}

int main() {
//	clock_t st = clock();
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int T = 1;
//	cin >> T;
	while (T--) {
		qiqi::main();
	}

//	cout << (double)(clock()-st)/CLOCKS_PER_SEC;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

aabb
ab

output:

aa*

result:

ok correct

Test #2:

score: 0
Accepted
time: 0ms
memory: 3520kb

input:

abaabaaabbbaabbb
abaabbbaabaaabbb

output:

abaaba*

result:

ok correct

Test #3:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

amuletofyendor
amuletofshmendor

output:

*y*

result:

ok correct

Test #4:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

spbau
spbsu

output:

*a*

result:

ok correct

Test #5:

score: 0
Accepted
time: 1ms
memory: 3632kb

input:

b
abaabaaaabaababbabba

output:

b

result:

ok correct

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 3632kb

input:

bbaabaaa
babbaabbbbababbaaab

output:

**

result:

wrong answer pattern matches the second string