# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096892 | thangdz2k7 | Copy and Paste 3 (JOI22_copypaste3) | C++17 | 241 ms | 110296 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 3e3;
const int mod = 1e9 + 7;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
#define next __next__
int n, a, b, c, f[N][N], next[N][N];
ll dp[N][N];
string s;
void solve(){
cin >> n >> s; s = ' ' + s;
cin >> a >> b >> c;
for (int i = n; i >= 1; -- i){
for (int j = n; j > i; -- j) if (s[i] != s[j]) f[i][j] = 0; else f[i][j] = f[i + 1][j + 1] + 1;
}
for (int i = 0; i <= n + 1; ++ i){
for (int j = 0; j <= n + 1; ++ j){
if (i > j) dp[i][j] = 0;
else dp[i][j] = ll(a) * (j - i + 1);
next[i][j] = n + 2;
}
}
for (int i = 1; i <= n; ++ i) {
int jj = i + 1;
for (int j = i; j <= n; ++ j){
if (jj > n) break;
if (jj <= j || f[i][jj] < (j - i + 1)){
-- j; ++ jj;
continue;
}
next[i][j - i + 1] = jj;
}
}
for (int i = n; i >= 1; -- i){
for (int j = i; j <= n; ++ j){
minl(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + a);
int p = i, le = j - i + 1, d = 1;
while (p < n + 1){
++ d; p = next[p][le]; int r = p + le - 1;
ll cost = ll(d) * c + b + ll(r - i + 1 - d * le) * a;
minl(dp[i][r], dp[i][j] + cost);
}
}
}
cout << dp[1][n];
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |