#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2502;
const int B = 5000011;
const int M1 = 1000000007;
const int M2 = 998244353;
int prv[N][N], nxt[N][N], h1[N][N], h2[N][N], dp[N][N];
int cv(char c) { return (c - 'a' + 1); }
bool same(int l, int r, int x) {
return (h1[l][l + x] == h1[r][r + x] && h2[l][l + x] == h2[r][r + x]);
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, a, b, c;
string s;
cin >> n >> s >> a >> b >> c;
s = "?" + s;
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = 1e18;
for (int i = 1; i <= n; i++) {
h1[i][i] = h2[i][i] = cv(s[i]);
dp[i][i] = a;
}
for (int itvl = 1; itvl <= n - 1; itvl++) {
for (int i = 1; i + itvl <= n; i++) {
int j = i + itvl;
h1[i][j] = (h1[i][j-1] * B + cv(s[j])) % M1;
h2[i][j] = (h2[i][j-1] * B + cv(s[j])) % M2;
}
}
for (int itvl = 0; itvl <= n - 1; itvl++) {
vector<pair<int, int>> h;
vector<vector<int>> posi(n+1);
for (int i = 1; i + itvl <= n; i++) h.push_back({h1[i][i + itvl], h2[i][i + itvl]});
sort(h.begin(), h.end());
h.erase(unique(h.begin(), h.end()), h.end());
int hi = 0;
for (int i = 1; i + itvl <= n; i++) {
pair<int, int> p = {h1[i][i + itvl], h2[i][i + itvl]};
int disc = lower_bound(h.begin(), h.end(), p) - h.begin() + 1;
hi = max(hi, disc);
posi[disc].push_back(i);
}
for (int i = 1; i <= hi; i++) {
auto& v = posi[i];
int ptr = 0;
for (int j = 1; j < v.size(); j++) {
while (ptr + 1 < j && v[ptr + 1] + itvl < v[j]) ptr++;
if (v[ptr] + itvl < v[j]) prv[v[j]][v[j] + itvl] = v[ptr];
}
ptr = v.size() - 1;
for (int j = v.size() - 2; j >= 0; j--) {
while (ptr - 1 > j && v[j] + itvl < v[ptr - 1]) ptr--;
if (v[j] + itvl < v[ptr]) nxt[v[j]][v[j] + itvl] = v[ptr];
}
}
}
for (int itvl = 0; itvl <= n - 1; itvl++) {
for (int i = 1; i + itvl <= n; i++) {
int j = i;
while (nxt[j][j + itvl]) {
int par = j + itvl, val = dp[i][j + itvl];
if (j == i) val += (b + c);
j = nxt[j][j + itvl];
dp[i][j + itvl] = min(dp[i][j + itvl], (j - par - 1) * a + val + c);
}
// j = i;
// while (prv[j][j + itvl]) {
// int par = j, val = dp[j][i + itvl];
// if (j == i) val += (b + c);
// j = prv[j][j + itvl];
// dp[j][i + itvl] = min(dp[j][i + itvl], (par - (j + itvl) - 1) * a + val + c);
// }
// if (i > 1) dp[i - 1][i + itvl] = min(dp[i - 1][i + itvl], dp[i][i + itvl] + a);
if (i + itvl < n) dp[i][i + itvl + 1] = min(dp[i][i + itvl + 1], dp[i][i + itvl] + a);
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = i; j <= n; j++) {
// cout << i << ' ' << j << ' ' << prv[i][j] << ' ' << nxt[i][j] << '\n';
// }
// cout << '\n';
// }
cout << dp[1][n] << '\n';
}
Compilation message
copypaste3.cpp: In function 'int32_t main()':
copypaste3.cpp:52:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j = 1; j < v.size(); j++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
49500 KB |
Output is correct |
2 |
Correct |
18 ms |
49500 KB |
Output is correct |
3 |
Correct |
18 ms |
49496 KB |
Output is correct |
4 |
Correct |
17 ms |
49464 KB |
Output is correct |
5 |
Correct |
18 ms |
49500 KB |
Output is correct |
6 |
Correct |
17 ms |
49244 KB |
Output is correct |
7 |
Correct |
19 ms |
49244 KB |
Output is correct |
8 |
Correct |
18 ms |
49500 KB |
Output is correct |
9 |
Correct |
17 ms |
49464 KB |
Output is correct |
10 |
Correct |
17 ms |
49500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
49500 KB |
Output is correct |
2 |
Correct |
17 ms |
49288 KB |
Output is correct |
3 |
Incorrect |
179 ms |
118016 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
49500 KB |
Output is correct |
2 |
Correct |
18 ms |
49500 KB |
Output is correct |
3 |
Correct |
18 ms |
49496 KB |
Output is correct |
4 |
Correct |
17 ms |
49464 KB |
Output is correct |
5 |
Correct |
18 ms |
49500 KB |
Output is correct |
6 |
Correct |
17 ms |
49244 KB |
Output is correct |
7 |
Correct |
19 ms |
49244 KB |
Output is correct |
8 |
Correct |
18 ms |
49500 KB |
Output is correct |
9 |
Correct |
17 ms |
49464 KB |
Output is correct |
10 |
Correct |
17 ms |
49500 KB |
Output is correct |
11 |
Incorrect |
18 ms |
49500 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
49500 KB |
Output is correct |
2 |
Correct |
18 ms |
49500 KB |
Output is correct |
3 |
Correct |
18 ms |
49496 KB |
Output is correct |
4 |
Correct |
17 ms |
49464 KB |
Output is correct |
5 |
Correct |
18 ms |
49500 KB |
Output is correct |
6 |
Correct |
17 ms |
49244 KB |
Output is correct |
7 |
Correct |
19 ms |
49244 KB |
Output is correct |
8 |
Correct |
18 ms |
49500 KB |
Output is correct |
9 |
Correct |
17 ms |
49464 KB |
Output is correct |
10 |
Correct |
17 ms |
49500 KB |
Output is correct |
11 |
Incorrect |
18 ms |
49500 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
49500 KB |
Output is correct |
2 |
Correct |
18 ms |
49500 KB |
Output is correct |
3 |
Correct |
18 ms |
49496 KB |
Output is correct |
4 |
Correct |
17 ms |
49464 KB |
Output is correct |
5 |
Correct |
18 ms |
49500 KB |
Output is correct |
6 |
Correct |
17 ms |
49244 KB |
Output is correct |
7 |
Correct |
19 ms |
49244 KB |
Output is correct |
8 |
Correct |
18 ms |
49500 KB |
Output is correct |
9 |
Correct |
17 ms |
49464 KB |
Output is correct |
10 |
Correct |
17 ms |
49500 KB |
Output is correct |
11 |
Incorrect |
18 ms |
49500 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
49500 KB |
Output is correct |
2 |
Correct |
18 ms |
49500 KB |
Output is correct |
3 |
Correct |
18 ms |
49496 KB |
Output is correct |
4 |
Correct |
17 ms |
49464 KB |
Output is correct |
5 |
Correct |
18 ms |
49500 KB |
Output is correct |
6 |
Correct |
17 ms |
49244 KB |
Output is correct |
7 |
Correct |
19 ms |
49244 KB |
Output is correct |
8 |
Correct |
18 ms |
49500 KB |
Output is correct |
9 |
Correct |
17 ms |
49464 KB |
Output is correct |
10 |
Correct |
17 ms |
49500 KB |
Output is correct |
11 |
Correct |
17 ms |
49500 KB |
Output is correct |
12 |
Correct |
17 ms |
49288 KB |
Output is correct |
13 |
Incorrect |
179 ms |
118016 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |