#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 2500 + 10;
const int ALPHABET = 26;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int n;
char s[MAXN];
int letter[MAXN][ALPHABET];
int fail[MAXN];
llong a, b, c;
llong dp[MAXN][MAXN];
llong cnt[MAXN][MAXN];
llong best[MAXN][MAXN];
std::vector <int> increase[MAXN][MAXN];
llong prefixHash[MAXN];
llong base[MAXN];
llong getHash(int l, int r)
{
llong res = prefixHash[r] + MOD - ((prefixHash[l - 1] * base[r - l + 1]) % MOD);
if (res >= MOD) res -= MOD;
return res;
}
void solve()
{
base[0] = 1;
for (int i = 1 ; i <= n ; ++i)
{
base[i] = (base[i - 1] * 27) % MOD;
}
for (int i = 1 ; i <= n ; ++i)
{
prefixHash[i] = prefixHash[i - 1] * 27;
prefixHash[i] += s[i] - 'a' + 1;
prefixHash[i] %= MOD;
}
for (int l = 1 ; l <= n ; ++l)
{
std::vector <int> v;
for (int r = l ; r <= n ; ++r)
{
if (s[l] == s[r])
{
// std::cout << "add: " << l << ' ' << r << '\n';
v.push_back(r);
increase[l][r].push_back(l);
}
}
fail[1] = 0;
for (int c = 0 ; c < ALPHABET ; ++c)
{
letter[1][c] = 0;
}
std::string curr; curr += s[l];
for (int r = l + 1 ; r <= n ; ++r)
{
curr += s[r];
fail[r - l + 1] = fail[r - l];
while (fail[r - l + 1] > 0 && s[l + fail[r - l + 1]] != s[r])
{
fail[r - l + 1] = fail[fail[r - l + 1] - 1];
}
if (s[l + fail[r - l + 1] + 1] == s[r])
{
fail[r - l + 1]++;
}
for (int c = 0 ; c < ALPHABET ; ++c)
{
if (s[l + letter[r - l][c]] == char('a' + c))
{
letter[r - l + 1][c] = letter[r - l][c] + 1;
} else
{
if (letter[r - l][c] == 0)
{
letter[r - l + 1][c] = 0;
} else
{
letter[r - l + 1][c] = letter[letter[r - l][c] - 1][c];
}
}
}
int last = l - 1;
std::vector <int> newV;
for (const int &left : v)
{
int len = r - l;
int idx = left + r - l;
int newLeft = left;
while (len > 0 && (newLeft <= last || s[l + idx - newLeft] != s[idx]))
{
len = letter[len][s[idx] - 'a'];
newLeft = idx - len;
}
// std::cout << "try: " << left << ' ' << newLeft << ' ' << idx << ' ' << last << " = " << getHash(newLeft, newLeft + r - l) << ' ' << getHash(l, r) << '\n';
if (newLeft > last && newLeft + r - l <= n && getHash(newLeft, newLeft + r - l) == getHash(l, r))
{
newV.push_back(newLeft);
last = newLeft + r - l;
}
}
// std::cout << "subarray: " << curr << '\n';
// std::cout << "at\n";
// for (const int &left : newV)
// {
// std::cout << left << ' ' << left + r - l << '\n';
// }
v = newV;
for (const int &left : v)
{
increase[l][left + r - l].push_back(r);
}
}
}
for (int len = 1 ; len <= n ; ++len)
{
for (int l = 1 ; l + len - 1 <= n ; ++l)
{
int r = l + len - 1;
best[l][r] = best[l][r - 1];
for (const int &idx : increase[l][r])
{
cnt[l][idx]++;
if (cnt[l][idx] > 1)
{
best[l][r] = std::max(best[l][r], cnt[l][idx] * (idx - l + 1) * a - b - c * cnt[l][idx] - dp[l][idx]);
}
}
dp[l][r] = a * (r - l + 1) - best[l][r];
if (len > 1)
{
dp[l][r] = std::min(dp[l][r], dp[l + 1][r] + a);
dp[l][r] = std::min(dp[l][r], dp[l][r - 1] + a);
}
}
}
std::cout << dp[1][n] << '\n';
}
void input()
{
std::cin >> n;
std::cin >> s + 1;
std::cin >> a >> b >> c;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
copypaste3.cpp: In function 'void input()':
copypaste3.cpp:168:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
168 | std::cin >> s + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
148304 KB |
Output is correct |
2 |
Correct |
50 ms |
148372 KB |
Output is correct |
3 |
Correct |
51 ms |
148316 KB |
Output is correct |
4 |
Correct |
52 ms |
148404 KB |
Output is correct |
5 |
Correct |
52 ms |
148308 KB |
Output is correct |
6 |
Correct |
50 ms |
148316 KB |
Output is correct |
7 |
Correct |
56 ms |
148300 KB |
Output is correct |
8 |
Correct |
54 ms |
148444 KB |
Output is correct |
9 |
Correct |
51 ms |
148276 KB |
Output is correct |
10 |
Correct |
51 ms |
148456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
148560 KB |
Output is correct |
2 |
Correct |
51 ms |
148448 KB |
Output is correct |
3 |
Correct |
1044 ms |
294632 KB |
Output is correct |
4 |
Correct |
1224 ms |
317868 KB |
Output is correct |
5 |
Correct |
1473 ms |
358744 KB |
Output is correct |
6 |
Correct |
1727 ms |
377936 KB |
Output is correct |
7 |
Correct |
2040 ms |
413820 KB |
Output is correct |
8 |
Correct |
2021 ms |
413108 KB |
Output is correct |
9 |
Correct |
2092 ms |
413120 KB |
Output is correct |
10 |
Correct |
39 ms |
152404 KB |
Output is correct |
11 |
Correct |
38 ms |
152408 KB |
Output is correct |
12 |
Correct |
37 ms |
152232 KB |
Output is correct |
13 |
Correct |
37 ms |
152468 KB |
Output is correct |
14 |
Correct |
37 ms |
152412 KB |
Output is correct |
15 |
Correct |
37 ms |
152404 KB |
Output is correct |
16 |
Correct |
37 ms |
152492 KB |
Output is correct |
17 |
Correct |
37 ms |
152468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
148304 KB |
Output is correct |
2 |
Correct |
50 ms |
148372 KB |
Output is correct |
3 |
Correct |
51 ms |
148316 KB |
Output is correct |
4 |
Correct |
52 ms |
148404 KB |
Output is correct |
5 |
Correct |
52 ms |
148308 KB |
Output is correct |
6 |
Correct |
50 ms |
148316 KB |
Output is correct |
7 |
Correct |
56 ms |
148300 KB |
Output is correct |
8 |
Correct |
54 ms |
148444 KB |
Output is correct |
9 |
Correct |
51 ms |
148276 KB |
Output is correct |
10 |
Correct |
51 ms |
148456 KB |
Output is correct |
11 |
Correct |
38 ms |
152404 KB |
Output is correct |
12 |
Correct |
38 ms |
152412 KB |
Output is correct |
13 |
Correct |
39 ms |
152536 KB |
Output is correct |
14 |
Correct |
37 ms |
152400 KB |
Output is correct |
15 |
Correct |
38 ms |
152400 KB |
Output is correct |
16 |
Correct |
37 ms |
152408 KB |
Output is correct |
17 |
Correct |
37 ms |
152412 KB |
Output is correct |
18 |
Correct |
37 ms |
152412 KB |
Output is correct |
19 |
Correct |
37 ms |
152412 KB |
Output is correct |
20 |
Correct |
36 ms |
152412 KB |
Output is correct |
21 |
Correct |
38 ms |
152488 KB |
Output is correct |
22 |
Correct |
41 ms |
152400 KB |
Output is correct |
23 |
Correct |
37 ms |
152412 KB |
Output is correct |
24 |
Correct |
36 ms |
152392 KB |
Output is correct |
25 |
Correct |
39 ms |
152916 KB |
Output is correct |
26 |
Correct |
38 ms |
152408 KB |
Output is correct |
27 |
Correct |
39 ms |
152584 KB |
Output is correct |
28 |
Correct |
39 ms |
152412 KB |
Output is correct |
29 |
Correct |
39 ms |
152356 KB |
Output is correct |
30 |
Correct |
37 ms |
152400 KB |
Output is correct |
31 |
Correct |
38 ms |
152448 KB |
Output is correct |
32 |
Correct |
38 ms |
152400 KB |
Output is correct |
33 |
Correct |
38 ms |
152412 KB |
Output is correct |
34 |
Correct |
37 ms |
152452 KB |
Output is correct |
35 |
Correct |
37 ms |
152444 KB |
Output is correct |
36 |
Correct |
37 ms |
152412 KB |
Output is correct |
37 |
Correct |
38 ms |
152404 KB |
Output is correct |
38 |
Correct |
37 ms |
152508 KB |
Output is correct |
39 |
Correct |
38 ms |
152360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
148304 KB |
Output is correct |
2 |
Correct |
50 ms |
148372 KB |
Output is correct |
3 |
Correct |
51 ms |
148316 KB |
Output is correct |
4 |
Correct |
52 ms |
148404 KB |
Output is correct |
5 |
Correct |
52 ms |
148308 KB |
Output is correct |
6 |
Correct |
50 ms |
148316 KB |
Output is correct |
7 |
Correct |
56 ms |
148300 KB |
Output is correct |
8 |
Correct |
54 ms |
148444 KB |
Output is correct |
9 |
Correct |
51 ms |
148276 KB |
Output is correct |
10 |
Correct |
51 ms |
148456 KB |
Output is correct |
11 |
Correct |
38 ms |
152404 KB |
Output is correct |
12 |
Correct |
38 ms |
152412 KB |
Output is correct |
13 |
Correct |
39 ms |
152536 KB |
Output is correct |
14 |
Correct |
37 ms |
152400 KB |
Output is correct |
15 |
Correct |
38 ms |
152400 KB |
Output is correct |
16 |
Correct |
37 ms |
152408 KB |
Output is correct |
17 |
Correct |
37 ms |
152412 KB |
Output is correct |
18 |
Correct |
37 ms |
152412 KB |
Output is correct |
19 |
Correct |
37 ms |
152412 KB |
Output is correct |
20 |
Correct |
36 ms |
152412 KB |
Output is correct |
21 |
Correct |
38 ms |
152488 KB |
Output is correct |
22 |
Correct |
41 ms |
152400 KB |
Output is correct |
23 |
Correct |
37 ms |
152412 KB |
Output is correct |
24 |
Correct |
36 ms |
152392 KB |
Output is correct |
25 |
Correct |
39 ms |
152916 KB |
Output is correct |
26 |
Correct |
38 ms |
152408 KB |
Output is correct |
27 |
Correct |
39 ms |
152584 KB |
Output is correct |
28 |
Correct |
39 ms |
152412 KB |
Output is correct |
29 |
Correct |
39 ms |
152356 KB |
Output is correct |
30 |
Correct |
37 ms |
152400 KB |
Output is correct |
31 |
Correct |
38 ms |
152448 KB |
Output is correct |
32 |
Correct |
38 ms |
152400 KB |
Output is correct |
33 |
Correct |
38 ms |
152412 KB |
Output is correct |
34 |
Correct |
37 ms |
152452 KB |
Output is correct |
35 |
Correct |
37 ms |
152444 KB |
Output is correct |
36 |
Correct |
37 ms |
152412 KB |
Output is correct |
37 |
Correct |
38 ms |
152404 KB |
Output is correct |
38 |
Correct |
37 ms |
152508 KB |
Output is correct |
39 |
Correct |
38 ms |
152360 KB |
Output is correct |
40 |
Correct |
38 ms |
152924 KB |
Output is correct |
41 |
Correct |
46 ms |
158544 KB |
Output is correct |
42 |
Correct |
43 ms |
158548 KB |
Output is correct |
43 |
Correct |
42 ms |
158548 KB |
Output is correct |
44 |
Correct |
41 ms |
158744 KB |
Output is correct |
45 |
Correct |
42 ms |
158548 KB |
Output is correct |
46 |
Correct |
41 ms |
158544 KB |
Output is correct |
47 |
Correct |
47 ms |
158548 KB |
Output is correct |
48 |
Correct |
43 ms |
158500 KB |
Output is correct |
49 |
Correct |
43 ms |
158600 KB |
Output is correct |
50 |
Correct |
42 ms |
158556 KB |
Output is correct |
51 |
Correct |
44 ms |
158556 KB |
Output is correct |
52 |
Correct |
44 ms |
158548 KB |
Output is correct |
53 |
Correct |
43 ms |
158552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
148304 KB |
Output is correct |
2 |
Correct |
50 ms |
148372 KB |
Output is correct |
3 |
Correct |
51 ms |
148316 KB |
Output is correct |
4 |
Correct |
52 ms |
148404 KB |
Output is correct |
5 |
Correct |
52 ms |
148308 KB |
Output is correct |
6 |
Correct |
50 ms |
148316 KB |
Output is correct |
7 |
Correct |
56 ms |
148300 KB |
Output is correct |
8 |
Correct |
54 ms |
148444 KB |
Output is correct |
9 |
Correct |
51 ms |
148276 KB |
Output is correct |
10 |
Correct |
51 ms |
148456 KB |
Output is correct |
11 |
Correct |
38 ms |
152404 KB |
Output is correct |
12 |
Correct |
38 ms |
152412 KB |
Output is correct |
13 |
Correct |
39 ms |
152536 KB |
Output is correct |
14 |
Correct |
37 ms |
152400 KB |
Output is correct |
15 |
Correct |
38 ms |
152400 KB |
Output is correct |
16 |
Correct |
37 ms |
152408 KB |
Output is correct |
17 |
Correct |
37 ms |
152412 KB |
Output is correct |
18 |
Correct |
37 ms |
152412 KB |
Output is correct |
19 |
Correct |
37 ms |
152412 KB |
Output is correct |
20 |
Correct |
36 ms |
152412 KB |
Output is correct |
21 |
Correct |
38 ms |
152488 KB |
Output is correct |
22 |
Correct |
41 ms |
152400 KB |
Output is correct |
23 |
Correct |
37 ms |
152412 KB |
Output is correct |
24 |
Correct |
36 ms |
152392 KB |
Output is correct |
25 |
Correct |
39 ms |
152916 KB |
Output is correct |
26 |
Correct |
38 ms |
152408 KB |
Output is correct |
27 |
Correct |
39 ms |
152584 KB |
Output is correct |
28 |
Correct |
39 ms |
152412 KB |
Output is correct |
29 |
Correct |
39 ms |
152356 KB |
Output is correct |
30 |
Correct |
37 ms |
152400 KB |
Output is correct |
31 |
Correct |
38 ms |
152448 KB |
Output is correct |
32 |
Correct |
38 ms |
152400 KB |
Output is correct |
33 |
Correct |
38 ms |
152412 KB |
Output is correct |
34 |
Correct |
37 ms |
152452 KB |
Output is correct |
35 |
Correct |
37 ms |
152444 KB |
Output is correct |
36 |
Correct |
37 ms |
152412 KB |
Output is correct |
37 |
Correct |
38 ms |
152404 KB |
Output is correct |
38 |
Correct |
37 ms |
152508 KB |
Output is correct |
39 |
Correct |
38 ms |
152360 KB |
Output is correct |
40 |
Correct |
38 ms |
152924 KB |
Output is correct |
41 |
Correct |
46 ms |
158544 KB |
Output is correct |
42 |
Correct |
43 ms |
158548 KB |
Output is correct |
43 |
Correct |
42 ms |
158548 KB |
Output is correct |
44 |
Correct |
41 ms |
158744 KB |
Output is correct |
45 |
Correct |
42 ms |
158548 KB |
Output is correct |
46 |
Correct |
41 ms |
158544 KB |
Output is correct |
47 |
Correct |
47 ms |
158548 KB |
Output is correct |
48 |
Correct |
43 ms |
158500 KB |
Output is correct |
49 |
Correct |
43 ms |
158600 KB |
Output is correct |
50 |
Correct |
42 ms |
158556 KB |
Output is correct |
51 |
Correct |
44 ms |
158556 KB |
Output is correct |
52 |
Correct |
44 ms |
158548 KB |
Output is correct |
53 |
Correct |
43 ms |
158552 KB |
Output is correct |
54 |
Correct |
61 ms |
168276 KB |
Output is correct |
55 |
Correct |
293 ms |
209496 KB |
Output is correct |
56 |
Correct |
162 ms |
201300 KB |
Output is correct |
57 |
Correct |
160 ms |
201300 KB |
Output is correct |
58 |
Correct |
127 ms |
201400 KB |
Output is correct |
59 |
Correct |
123 ms |
201324 KB |
Output is correct |
60 |
Correct |
122 ms |
201592 KB |
Output is correct |
61 |
Correct |
151 ms |
201444 KB |
Output is correct |
62 |
Incorrect |
237 ms |
205612 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
148304 KB |
Output is correct |
2 |
Correct |
50 ms |
148372 KB |
Output is correct |
3 |
Correct |
51 ms |
148316 KB |
Output is correct |
4 |
Correct |
52 ms |
148404 KB |
Output is correct |
5 |
Correct |
52 ms |
148308 KB |
Output is correct |
6 |
Correct |
50 ms |
148316 KB |
Output is correct |
7 |
Correct |
56 ms |
148300 KB |
Output is correct |
8 |
Correct |
54 ms |
148444 KB |
Output is correct |
9 |
Correct |
51 ms |
148276 KB |
Output is correct |
10 |
Correct |
51 ms |
148456 KB |
Output is correct |
11 |
Correct |
50 ms |
148560 KB |
Output is correct |
12 |
Correct |
51 ms |
148448 KB |
Output is correct |
13 |
Correct |
1044 ms |
294632 KB |
Output is correct |
14 |
Correct |
1224 ms |
317868 KB |
Output is correct |
15 |
Correct |
1473 ms |
358744 KB |
Output is correct |
16 |
Correct |
1727 ms |
377936 KB |
Output is correct |
17 |
Correct |
2040 ms |
413820 KB |
Output is correct |
18 |
Correct |
2021 ms |
413108 KB |
Output is correct |
19 |
Correct |
2092 ms |
413120 KB |
Output is correct |
20 |
Correct |
39 ms |
152404 KB |
Output is correct |
21 |
Correct |
38 ms |
152408 KB |
Output is correct |
22 |
Correct |
37 ms |
152232 KB |
Output is correct |
23 |
Correct |
37 ms |
152468 KB |
Output is correct |
24 |
Correct |
37 ms |
152412 KB |
Output is correct |
25 |
Correct |
37 ms |
152404 KB |
Output is correct |
26 |
Correct |
37 ms |
152492 KB |
Output is correct |
27 |
Correct |
37 ms |
152468 KB |
Output is correct |
28 |
Correct |
38 ms |
152404 KB |
Output is correct |
29 |
Correct |
38 ms |
152412 KB |
Output is correct |
30 |
Correct |
39 ms |
152536 KB |
Output is correct |
31 |
Correct |
37 ms |
152400 KB |
Output is correct |
32 |
Correct |
38 ms |
152400 KB |
Output is correct |
33 |
Correct |
37 ms |
152408 KB |
Output is correct |
34 |
Correct |
37 ms |
152412 KB |
Output is correct |
35 |
Correct |
37 ms |
152412 KB |
Output is correct |
36 |
Correct |
37 ms |
152412 KB |
Output is correct |
37 |
Correct |
36 ms |
152412 KB |
Output is correct |
38 |
Correct |
38 ms |
152488 KB |
Output is correct |
39 |
Correct |
41 ms |
152400 KB |
Output is correct |
40 |
Correct |
37 ms |
152412 KB |
Output is correct |
41 |
Correct |
36 ms |
152392 KB |
Output is correct |
42 |
Correct |
39 ms |
152916 KB |
Output is correct |
43 |
Correct |
38 ms |
152408 KB |
Output is correct |
44 |
Correct |
39 ms |
152584 KB |
Output is correct |
45 |
Correct |
39 ms |
152412 KB |
Output is correct |
46 |
Correct |
39 ms |
152356 KB |
Output is correct |
47 |
Correct |
37 ms |
152400 KB |
Output is correct |
48 |
Correct |
38 ms |
152448 KB |
Output is correct |
49 |
Correct |
38 ms |
152400 KB |
Output is correct |
50 |
Correct |
38 ms |
152412 KB |
Output is correct |
51 |
Correct |
37 ms |
152452 KB |
Output is correct |
52 |
Correct |
37 ms |
152444 KB |
Output is correct |
53 |
Correct |
37 ms |
152412 KB |
Output is correct |
54 |
Correct |
38 ms |
152404 KB |
Output is correct |
55 |
Correct |
37 ms |
152508 KB |
Output is correct |
56 |
Correct |
38 ms |
152360 KB |
Output is correct |
57 |
Correct |
38 ms |
152924 KB |
Output is correct |
58 |
Correct |
46 ms |
158544 KB |
Output is correct |
59 |
Correct |
43 ms |
158548 KB |
Output is correct |
60 |
Correct |
42 ms |
158548 KB |
Output is correct |
61 |
Correct |
41 ms |
158744 KB |
Output is correct |
62 |
Correct |
42 ms |
158548 KB |
Output is correct |
63 |
Correct |
41 ms |
158544 KB |
Output is correct |
64 |
Correct |
47 ms |
158548 KB |
Output is correct |
65 |
Correct |
43 ms |
158500 KB |
Output is correct |
66 |
Correct |
43 ms |
158600 KB |
Output is correct |
67 |
Correct |
42 ms |
158556 KB |
Output is correct |
68 |
Correct |
44 ms |
158556 KB |
Output is correct |
69 |
Correct |
44 ms |
158548 KB |
Output is correct |
70 |
Correct |
43 ms |
158552 KB |
Output is correct |
71 |
Correct |
61 ms |
168276 KB |
Output is correct |
72 |
Correct |
293 ms |
209496 KB |
Output is correct |
73 |
Correct |
162 ms |
201300 KB |
Output is correct |
74 |
Correct |
160 ms |
201300 KB |
Output is correct |
75 |
Correct |
127 ms |
201400 KB |
Output is correct |
76 |
Correct |
123 ms |
201324 KB |
Output is correct |
77 |
Correct |
122 ms |
201592 KB |
Output is correct |
78 |
Correct |
151 ms |
201444 KB |
Output is correct |
79 |
Incorrect |
237 ms |
205612 KB |
Output isn't correct |
80 |
Halted |
0 ms |
0 KB |
- |