#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <typename T>
using ve = vector<T>;
const int N = 2505;
const ll B = 137, M = 1000000007;
int n, lcp[N][N];
ll dp[N][N], a, b, c;
char s[N];
ll hs[N], bp[N], ibp[N];
ll bpow(ll a, ll b) {
if (!b) return 1;
ll r { bpow(a, b / 2) };
if (b & 1) return r * r % M * a % M;
return r * r % M;
}
void do_hash() {
bp[0] = ibp[0] = 1;
for (int i = 1; i <= n; ++i) bp[i] = bp[i - 1] * B % M, ibp[i] = bpow(bp[i], M - 2);
for (int i = 1; i <= n; ++i) hs[i] = bp[i] * s[i] % M, hs[i] = (hs[i] + hs[i - 1]) % M;
}
ll get_hash(int l, int r) {
return ibp[l] * (hs[r] - hs[l - 1] + M) % M;
}
map<ll, ve<int>> oo, op[N];
int main() {
scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
do_hash();
memset(dp, 63, sizeof dp);
for (int i = 1; i <= n; ++i) {
lcp[i][i] = n - i + 1;
lcp[n][i] = lcp[i][n] = (s[i] == s[n]);
dp[i][i] = a;
for (int j = i; j <= n; ++j)
oo[get_hash(i, j)].push_back(i);
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
ll x = get_hash(i, j), at = i;
auto &v = oo[x];
auto it = lower_bound(v.begin(), v.end(), at);
while (it != v.end()) {
op[i][x].push_back(*it);
at += (j - i + 1);
it = lower_bound(v.begin(), v.end(), at);
}
}
}
for (int i = n - 1; i >= 1; --i) {
for (int j = n - 1; j >= 1; --j) {
lcp[i][j] = max(lcp[i][j], lcp[i + 1][j + 1] + (s[i] == s[j]));
}
}
for (int l = 2; l <= n; ++l) {
for (int i = 1, j = l; j <= n; ++i, ++j) {
for (int k = i; k <= j; ++k) {
int len = k - i + 1;
ll x = get_hash(i, k);
auto &oc = op[i][x];
int cnt = upper_bound(oc.begin(), oc.end(), j - len + 1) - lower_bound(oc.begin(), oc.end(), i);
int lef = j - i + 1 - cnt * len;
dp[i][j] = min(dp[i][j], dp[i][k] + lef * a + cnt * c + b);
}
dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
}
}
printf("%lld", dp[1][n]);
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49812 KB |
Output is correct |
8 |
Correct |
5 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
49756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
51804 KB |
Output is correct |
2 |
Correct |
6 ms |
49748 KB |
Output is correct |
3 |
Execution timed out |
3104 ms |
309988 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49812 KB |
Output is correct |
8 |
Correct |
5 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
49756 KB |
Output is correct |
11 |
Incorrect |
6 ms |
49756 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49812 KB |
Output is correct |
8 |
Correct |
5 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
49756 KB |
Output is correct |
11 |
Incorrect |
6 ms |
49756 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49812 KB |
Output is correct |
8 |
Correct |
5 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
49756 KB |
Output is correct |
11 |
Incorrect |
6 ms |
49756 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49812 KB |
Output is correct |
8 |
Correct |
5 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
49756 KB |
Output is correct |
11 |
Correct |
7 ms |
51804 KB |
Output is correct |
12 |
Correct |
6 ms |
49748 KB |
Output is correct |
13 |
Execution timed out |
3104 ms |
309988 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |