QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#196686 | #7234. Pencil of Wishing | jeffqi | WA | 0ms | 3876kb | C++20 | 2.6kb | 2023-10-01 21:27:45 | 2023-10-01 21:27:46 |
Judging History
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;
}
string sans;
auto [p,q] = pans;
int k = ans;
if (p+k-f[p][q] != n) {
sans += '*';
k--;
}
while (k) {
for (int i = p+k-f[p][q]; i >= p; i--) {
sans += s[i];
}
k = f[p][q];
if (p) {
sans += '*';
k--;
}
while (p && f[p][q] == f[p-1][q]) {
p--;
}
tie(p,q) = g[p][q];
}
reverse(all(sans));
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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3876kb
input:
aabb ab
output:
aa*
result:
ok correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
abaabaaabbbaabbb abaabbbaabaaabbb
output:
abaaba*
result:
ok correct
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3652kb
input:
amuletofyendor amuletofshmendor
output:
*ye*
result:
wrong answer jury has better answer