#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <map>
typedef long long llong;
const int MAXN = 2500 + 10;
const int ALPHABET = 26;
const int MOD2 = 998244353;
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;
int next[MAXN][MAXN];
llong dp[MAXN][MAXN];
llong cnt[MAXN][MAXN];
llong best[MAXN][MAXN];
std::vector <int> increase[MAXN][MAXN];
llong prefixHash[MAXN];
llong prefixHash2[MAXN];
llong base[MAXN];
llong base2[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;
}
llong getHash2(int l, int r)
{
llong res = prefixHash2[r] + MOD - ((prefixHash2[l - 1] * base[r - l + 1]) % MOD2);
if (res >= MOD2) res -= MOD2;
return res;
}
void solve()
{
base[0] = 1;
base2[0] = 1;
for (int i = 1 ; i <= n ; ++i)
{
base[i] = (base[i - 1] * 27) % MOD;
base2[i] = (base2[i - 1] * 27) % MOD2;
}
for (int i = 1 ; i <= n ; ++i)
{
prefixHash[i] = prefixHash[i - 1] * 27;
prefixHash[i] += s[i] - 'a' + 1;
prefixHash[i] %= MOD;
}
for (int i = 1 ; i <= n ; ++i)
{
prefixHash2[i] = prefixHash2[i - 1] * 27;
prefixHash2[i] += s[i] - 'a' + 1;
prefixHash2[i] %= MOD2;
}
for (int len = 1 ; len <= n ; ++len)
{
std::map <std::pair <int,int> ,int> map;
for (int pos = n - len + 1 ; pos >= 1 ; --pos)
{
int r = pos + len - 1;
next[pos][r] = map[{getHash(pos, r), getHash2(pos, r)}];
if (r + len - 1 <= n)
{
map[{getHash(r, r + len - 1), getHash2(r, r + len - 1)}] = r;
}
}
}
for (int l = 1 ; l <= n ; ++l)
{
for (int r = l ; r <= n ; ++r)
{
int left = l;
int len = r - l;
do
{
increase[l][left + len].push_back(r);
left = next[left][left + len];
} while (left != 0);
}
}
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;
// for (int i = 1 ; i <= n ; ++i)
// {
// if (i % 3 == 1) assert(s[i] == 'n');
// if (i % 3 == 2) assert(s[i] == 'q');
// if (i % 3 == 0) assert(s[i] == 'v');
// }
}
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:127:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
127 | std::cin >> s + 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154460 KB |
Output is correct |
2 |
Correct |
35 ms |
154456 KB |
Output is correct |
3 |
Correct |
34 ms |
154460 KB |
Output is correct |
4 |
Correct |
34 ms |
154460 KB |
Output is correct |
5 |
Correct |
33 ms |
154460 KB |
Output is correct |
6 |
Correct |
34 ms |
154452 KB |
Output is correct |
7 |
Correct |
33 ms |
154348 KB |
Output is correct |
8 |
Correct |
34 ms |
154420 KB |
Output is correct |
9 |
Correct |
34 ms |
154460 KB |
Output is correct |
10 |
Correct |
33 ms |
154452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154452 KB |
Output is correct |
2 |
Correct |
34 ms |
154460 KB |
Output is correct |
3 |
Incorrect |
684 ms |
302592 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154460 KB |
Output is correct |
2 |
Correct |
35 ms |
154456 KB |
Output is correct |
3 |
Correct |
34 ms |
154460 KB |
Output is correct |
4 |
Correct |
34 ms |
154460 KB |
Output is correct |
5 |
Correct |
33 ms |
154460 KB |
Output is correct |
6 |
Correct |
34 ms |
154452 KB |
Output is correct |
7 |
Correct |
33 ms |
154348 KB |
Output is correct |
8 |
Correct |
34 ms |
154420 KB |
Output is correct |
9 |
Correct |
34 ms |
154460 KB |
Output is correct |
10 |
Correct |
33 ms |
154452 KB |
Output is correct |
11 |
Correct |
34 ms |
154384 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
35 ms |
154460 KB |
Output is correct |
14 |
Correct |
35 ms |
154248 KB |
Output is correct |
15 |
Correct |
35 ms |
154456 KB |
Output is correct |
16 |
Correct |
35 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154508 KB |
Output is correct |
18 |
Correct |
34 ms |
154312 KB |
Output is correct |
19 |
Correct |
34 ms |
154452 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154484 KB |
Output is correct |
22 |
Correct |
35 ms |
154460 KB |
Output is correct |
23 |
Correct |
34 ms |
154604 KB |
Output is correct |
24 |
Correct |
34 ms |
154608 KB |
Output is correct |
25 |
Correct |
33 ms |
154604 KB |
Output is correct |
26 |
Correct |
36 ms |
154460 KB |
Output is correct |
27 |
Correct |
34 ms |
154448 KB |
Output is correct |
28 |
Correct |
35 ms |
154456 KB |
Output is correct |
29 |
Correct |
34 ms |
154448 KB |
Output is correct |
30 |
Correct |
35 ms |
154460 KB |
Output is correct |
31 |
Correct |
34 ms |
154456 KB |
Output is correct |
32 |
Incorrect |
34 ms |
154456 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154460 KB |
Output is correct |
2 |
Correct |
35 ms |
154456 KB |
Output is correct |
3 |
Correct |
34 ms |
154460 KB |
Output is correct |
4 |
Correct |
34 ms |
154460 KB |
Output is correct |
5 |
Correct |
33 ms |
154460 KB |
Output is correct |
6 |
Correct |
34 ms |
154452 KB |
Output is correct |
7 |
Correct |
33 ms |
154348 KB |
Output is correct |
8 |
Correct |
34 ms |
154420 KB |
Output is correct |
9 |
Correct |
34 ms |
154460 KB |
Output is correct |
10 |
Correct |
33 ms |
154452 KB |
Output is correct |
11 |
Correct |
34 ms |
154384 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
35 ms |
154460 KB |
Output is correct |
14 |
Correct |
35 ms |
154248 KB |
Output is correct |
15 |
Correct |
35 ms |
154456 KB |
Output is correct |
16 |
Correct |
35 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154508 KB |
Output is correct |
18 |
Correct |
34 ms |
154312 KB |
Output is correct |
19 |
Correct |
34 ms |
154452 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154484 KB |
Output is correct |
22 |
Correct |
35 ms |
154460 KB |
Output is correct |
23 |
Correct |
34 ms |
154604 KB |
Output is correct |
24 |
Correct |
34 ms |
154608 KB |
Output is correct |
25 |
Correct |
33 ms |
154604 KB |
Output is correct |
26 |
Correct |
36 ms |
154460 KB |
Output is correct |
27 |
Correct |
34 ms |
154448 KB |
Output is correct |
28 |
Correct |
35 ms |
154456 KB |
Output is correct |
29 |
Correct |
34 ms |
154448 KB |
Output is correct |
30 |
Correct |
35 ms |
154460 KB |
Output is correct |
31 |
Correct |
34 ms |
154456 KB |
Output is correct |
32 |
Incorrect |
34 ms |
154456 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154460 KB |
Output is correct |
2 |
Correct |
35 ms |
154456 KB |
Output is correct |
3 |
Correct |
34 ms |
154460 KB |
Output is correct |
4 |
Correct |
34 ms |
154460 KB |
Output is correct |
5 |
Correct |
33 ms |
154460 KB |
Output is correct |
6 |
Correct |
34 ms |
154452 KB |
Output is correct |
7 |
Correct |
33 ms |
154348 KB |
Output is correct |
8 |
Correct |
34 ms |
154420 KB |
Output is correct |
9 |
Correct |
34 ms |
154460 KB |
Output is correct |
10 |
Correct |
33 ms |
154452 KB |
Output is correct |
11 |
Correct |
34 ms |
154384 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
35 ms |
154460 KB |
Output is correct |
14 |
Correct |
35 ms |
154248 KB |
Output is correct |
15 |
Correct |
35 ms |
154456 KB |
Output is correct |
16 |
Correct |
35 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154508 KB |
Output is correct |
18 |
Correct |
34 ms |
154312 KB |
Output is correct |
19 |
Correct |
34 ms |
154452 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154484 KB |
Output is correct |
22 |
Correct |
35 ms |
154460 KB |
Output is correct |
23 |
Correct |
34 ms |
154604 KB |
Output is correct |
24 |
Correct |
34 ms |
154608 KB |
Output is correct |
25 |
Correct |
33 ms |
154604 KB |
Output is correct |
26 |
Correct |
36 ms |
154460 KB |
Output is correct |
27 |
Correct |
34 ms |
154448 KB |
Output is correct |
28 |
Correct |
35 ms |
154456 KB |
Output is correct |
29 |
Correct |
34 ms |
154448 KB |
Output is correct |
30 |
Correct |
35 ms |
154460 KB |
Output is correct |
31 |
Correct |
34 ms |
154456 KB |
Output is correct |
32 |
Incorrect |
34 ms |
154456 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
154460 KB |
Output is correct |
2 |
Correct |
35 ms |
154456 KB |
Output is correct |
3 |
Correct |
34 ms |
154460 KB |
Output is correct |
4 |
Correct |
34 ms |
154460 KB |
Output is correct |
5 |
Correct |
33 ms |
154460 KB |
Output is correct |
6 |
Correct |
34 ms |
154452 KB |
Output is correct |
7 |
Correct |
33 ms |
154348 KB |
Output is correct |
8 |
Correct |
34 ms |
154420 KB |
Output is correct |
9 |
Correct |
34 ms |
154460 KB |
Output is correct |
10 |
Correct |
33 ms |
154452 KB |
Output is correct |
11 |
Correct |
34 ms |
154452 KB |
Output is correct |
12 |
Correct |
34 ms |
154460 KB |
Output is correct |
13 |
Incorrect |
684 ms |
302592 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |