This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <deque>
#include <string>
#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <typename T>
using ve = vector<T>;
template <typename T>
using bs = basic_string<T>;
const int N = 2505;
int n, nxt[N][N + N], z[N];
void do_z(char *s, int n) {
for (int i = 0; i < n; ++i) z[i] = 0;
z[0] = n;
for (int i = 1, l {}, r { 1 }; i < n; ++i) {
if (i < r) z[i] = min(z[i - l], r - i);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] > r) l = i, r = i + z[i];
}
}
ll dp[N][N], a, b, c;
char s[N];
void init() {
memset(dp, 63, sizeof dp);
memset(nxt, 63, sizeof nxt);
for (int i = 1; i <= n; ++i)
dp[i][i] = a;
for (int i = 1; i <= n; ++i) {
do_z(s + i, n - i + 1);
for (int p = i, j = i; j <= n; ++j) {
while (p <= n && (p <= j || z[p - i] < j - i + 1)) ++p;
nxt[i][j] = p;
}
}
}
int main() {
scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
init();
for (int l = 1; l <= n; ++l) {
for (int i = 1, j = l; j <= n; ++i, ++j) {
dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a);
for (int at = i, cnt = 1; at <= n; ++cnt, at = nxt[at][at + l - 1]) {
int jj = at + l - 1;
dp[i][jj] = min(dp[i][jj], dp[i][j] + b + cnt * c + (jj - i + 1 - cnt * l) * a);
}
}
}
printf("%lld", dp[1][n]);
}
Compilation message (stderr)
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |