#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], 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() {
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
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |