#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] + MOD2 - ((prefixHash2[l - 1] * base2[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;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
154476 KB |
Output is correct |
2 |
Correct |
35 ms |
154460 KB |
Output is correct |
3 |
Correct |
35 ms |
154460 KB |
Output is correct |
4 |
Correct |
38 ms |
154456 KB |
Output is correct |
5 |
Correct |
36 ms |
154464 KB |
Output is correct |
6 |
Correct |
34 ms |
154460 KB |
Output is correct |
7 |
Correct |
33 ms |
154460 KB |
Output is correct |
8 |
Correct |
34 ms |
154456 KB |
Output is correct |
9 |
Correct |
33 ms |
154452 KB |
Output is correct |
10 |
Correct |
34 ms |
154460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
154448 KB |
Output is correct |
2 |
Correct |
34 ms |
154456 KB |
Output is correct |
3 |
Correct |
729 ms |
329492 KB |
Output is correct |
4 |
Correct |
857 ms |
356088 KB |
Output is correct |
5 |
Correct |
1027 ms |
384448 KB |
Output is correct |
6 |
Correct |
1201 ms |
417884 KB |
Output is correct |
7 |
Correct |
1483 ms |
452372 KB |
Output is correct |
8 |
Correct |
1402 ms |
452388 KB |
Output is correct |
9 |
Correct |
1405 ms |
452460 KB |
Output is correct |
10 |
Correct |
33 ms |
154352 KB |
Output is correct |
11 |
Correct |
34 ms |
154452 KB |
Output is correct |
12 |
Correct |
34 ms |
154456 KB |
Output is correct |
13 |
Correct |
34 ms |
154484 KB |
Output is correct |
14 |
Correct |
34 ms |
154508 KB |
Output is correct |
15 |
Correct |
37 ms |
154568 KB |
Output is correct |
16 |
Correct |
34 ms |
154456 KB |
Output is correct |
17 |
Correct |
38 ms |
154448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
154476 KB |
Output is correct |
2 |
Correct |
35 ms |
154460 KB |
Output is correct |
3 |
Correct |
35 ms |
154460 KB |
Output is correct |
4 |
Correct |
38 ms |
154456 KB |
Output is correct |
5 |
Correct |
36 ms |
154464 KB |
Output is correct |
6 |
Correct |
34 ms |
154460 KB |
Output is correct |
7 |
Correct |
33 ms |
154460 KB |
Output is correct |
8 |
Correct |
34 ms |
154456 KB |
Output is correct |
9 |
Correct |
33 ms |
154452 KB |
Output is correct |
10 |
Correct |
34 ms |
154460 KB |
Output is correct |
11 |
Correct |
34 ms |
154460 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
33 ms |
154424 KB |
Output is correct |
14 |
Correct |
36 ms |
154488 KB |
Output is correct |
15 |
Correct |
34 ms |
154448 KB |
Output is correct |
16 |
Correct |
36 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154460 KB |
Output is correct |
18 |
Correct |
34 ms |
154448 KB |
Output is correct |
19 |
Correct |
34 ms |
154712 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154460 KB |
Output is correct |
22 |
Correct |
33 ms |
154448 KB |
Output is correct |
23 |
Correct |
34 ms |
154448 KB |
Output is correct |
24 |
Correct |
36 ms |
154460 KB |
Output is correct |
25 |
Correct |
34 ms |
154452 KB |
Output is correct |
26 |
Correct |
34 ms |
154460 KB |
Output is correct |
27 |
Correct |
37 ms |
154456 KB |
Output is correct |
28 |
Correct |
33 ms |
154596 KB |
Output is correct |
29 |
Correct |
34 ms |
154388 KB |
Output is correct |
30 |
Correct |
35 ms |
154712 KB |
Output is correct |
31 |
Correct |
34 ms |
154460 KB |
Output is correct |
32 |
Correct |
34 ms |
154380 KB |
Output is correct |
33 |
Correct |
34 ms |
154456 KB |
Output is correct |
34 |
Correct |
34 ms |
154716 KB |
Output is correct |
35 |
Correct |
35 ms |
154464 KB |
Output is correct |
36 |
Correct |
33 ms |
154468 KB |
Output is correct |
37 |
Correct |
35 ms |
154456 KB |
Output is correct |
38 |
Correct |
38 ms |
154496 KB |
Output is correct |
39 |
Correct |
34 ms |
154460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
154476 KB |
Output is correct |
2 |
Correct |
35 ms |
154460 KB |
Output is correct |
3 |
Correct |
35 ms |
154460 KB |
Output is correct |
4 |
Correct |
38 ms |
154456 KB |
Output is correct |
5 |
Correct |
36 ms |
154464 KB |
Output is correct |
6 |
Correct |
34 ms |
154460 KB |
Output is correct |
7 |
Correct |
33 ms |
154460 KB |
Output is correct |
8 |
Correct |
34 ms |
154456 KB |
Output is correct |
9 |
Correct |
33 ms |
154452 KB |
Output is correct |
10 |
Correct |
34 ms |
154460 KB |
Output is correct |
11 |
Correct |
34 ms |
154460 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
33 ms |
154424 KB |
Output is correct |
14 |
Correct |
36 ms |
154488 KB |
Output is correct |
15 |
Correct |
34 ms |
154448 KB |
Output is correct |
16 |
Correct |
36 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154460 KB |
Output is correct |
18 |
Correct |
34 ms |
154448 KB |
Output is correct |
19 |
Correct |
34 ms |
154712 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154460 KB |
Output is correct |
22 |
Correct |
33 ms |
154448 KB |
Output is correct |
23 |
Correct |
34 ms |
154448 KB |
Output is correct |
24 |
Correct |
36 ms |
154460 KB |
Output is correct |
25 |
Correct |
34 ms |
154452 KB |
Output is correct |
26 |
Correct |
34 ms |
154460 KB |
Output is correct |
27 |
Correct |
37 ms |
154456 KB |
Output is correct |
28 |
Correct |
33 ms |
154596 KB |
Output is correct |
29 |
Correct |
34 ms |
154388 KB |
Output is correct |
30 |
Correct |
35 ms |
154712 KB |
Output is correct |
31 |
Correct |
34 ms |
154460 KB |
Output is correct |
32 |
Correct |
34 ms |
154380 KB |
Output is correct |
33 |
Correct |
34 ms |
154456 KB |
Output is correct |
34 |
Correct |
34 ms |
154716 KB |
Output is correct |
35 |
Correct |
35 ms |
154464 KB |
Output is correct |
36 |
Correct |
33 ms |
154468 KB |
Output is correct |
37 |
Correct |
35 ms |
154456 KB |
Output is correct |
38 |
Correct |
38 ms |
154496 KB |
Output is correct |
39 |
Correct |
34 ms |
154460 KB |
Output is correct |
40 |
Correct |
34 ms |
154716 KB |
Output is correct |
41 |
Correct |
41 ms |
162640 KB |
Output is correct |
42 |
Correct |
40 ms |
162644 KB |
Output is correct |
43 |
Correct |
43 ms |
162644 KB |
Output is correct |
44 |
Correct |
40 ms |
162652 KB |
Output is correct |
45 |
Correct |
40 ms |
162652 KB |
Output is correct |
46 |
Correct |
38 ms |
162608 KB |
Output is correct |
47 |
Correct |
40 ms |
162644 KB |
Output is correct |
48 |
Correct |
39 ms |
162412 KB |
Output is correct |
49 |
Correct |
42 ms |
162440 KB |
Output is correct |
50 |
Correct |
40 ms |
162640 KB |
Output is correct |
51 |
Correct |
39 ms |
162640 KB |
Output is correct |
52 |
Correct |
44 ms |
162644 KB |
Output is correct |
53 |
Correct |
39 ms |
162536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
154476 KB |
Output is correct |
2 |
Correct |
35 ms |
154460 KB |
Output is correct |
3 |
Correct |
35 ms |
154460 KB |
Output is correct |
4 |
Correct |
38 ms |
154456 KB |
Output is correct |
5 |
Correct |
36 ms |
154464 KB |
Output is correct |
6 |
Correct |
34 ms |
154460 KB |
Output is correct |
7 |
Correct |
33 ms |
154460 KB |
Output is correct |
8 |
Correct |
34 ms |
154456 KB |
Output is correct |
9 |
Correct |
33 ms |
154452 KB |
Output is correct |
10 |
Correct |
34 ms |
154460 KB |
Output is correct |
11 |
Correct |
34 ms |
154460 KB |
Output is correct |
12 |
Correct |
35 ms |
154460 KB |
Output is correct |
13 |
Correct |
33 ms |
154424 KB |
Output is correct |
14 |
Correct |
36 ms |
154488 KB |
Output is correct |
15 |
Correct |
34 ms |
154448 KB |
Output is correct |
16 |
Correct |
36 ms |
154452 KB |
Output is correct |
17 |
Correct |
34 ms |
154460 KB |
Output is correct |
18 |
Correct |
34 ms |
154448 KB |
Output is correct |
19 |
Correct |
34 ms |
154712 KB |
Output is correct |
20 |
Correct |
34 ms |
154460 KB |
Output is correct |
21 |
Correct |
34 ms |
154460 KB |
Output is correct |
22 |
Correct |
33 ms |
154448 KB |
Output is correct |
23 |
Correct |
34 ms |
154448 KB |
Output is correct |
24 |
Correct |
36 ms |
154460 KB |
Output is correct |
25 |
Correct |
34 ms |
154452 KB |
Output is correct |
26 |
Correct |
34 ms |
154460 KB |
Output is correct |
27 |
Correct |
37 ms |
154456 KB |
Output is correct |
28 |
Correct |
33 ms |
154596 KB |
Output is correct |
29 |
Correct |
34 ms |
154388 KB |
Output is correct |
30 |
Correct |
35 ms |
154712 KB |
Output is correct |
31 |
Correct |
34 ms |
154460 KB |
Output is correct |
32 |
Correct |
34 ms |
154380 KB |
Output is correct |
33 |
Correct |
34 ms |
154456 KB |
Output is correct |
34 |
Correct |
34 ms |
154716 KB |
Output is correct |
35 |
Correct |
35 ms |
154464 KB |
Output is correct |
36 |
Correct |
33 ms |
154468 KB |
Output is correct |
37 |
Correct |
35 ms |
154456 KB |
Output is correct |
38 |
Correct |
38 ms |
154496 KB |
Output is correct |
39 |
Correct |
34 ms |
154460 KB |
Output is correct |
40 |
Correct |
34 ms |
154716 KB |
Output is correct |
41 |
Correct |
41 ms |
162640 KB |
Output is correct |
42 |
Correct |
40 ms |
162644 KB |
Output is correct |
43 |
Correct |
43 ms |
162644 KB |
Output is correct |
44 |
Correct |
40 ms |
162652 KB |
Output is correct |
45 |
Correct |
40 ms |
162652 KB |
Output is correct |
46 |
Correct |
38 ms |
162608 KB |
Output is correct |
47 |
Correct |
40 ms |
162644 KB |
Output is correct |
48 |
Correct |
39 ms |
162412 KB |
Output is correct |
49 |
Correct |
42 ms |
162440 KB |
Output is correct |
50 |
Correct |
40 ms |
162640 KB |
Output is correct |
51 |
Correct |
39 ms |
162640 KB |
Output is correct |
52 |
Correct |
44 ms |
162644 KB |
Output is correct |
53 |
Correct |
39 ms |
162536 KB |
Output is correct |
54 |
Correct |
63 ms |
174164 KB |
Output is correct |
55 |
Correct |
225 ms |
219732 KB |
Output is correct |
56 |
Correct |
178 ms |
211736 KB |
Output is correct |
57 |
Correct |
171 ms |
211540 KB |
Output is correct |
58 |
Correct |
162 ms |
211540 KB |
Output is correct |
59 |
Correct |
160 ms |
211396 KB |
Output is correct |
60 |
Correct |
157 ms |
211828 KB |
Output is correct |
61 |
Correct |
157 ms |
211608 KB |
Output is correct |
62 |
Correct |
191 ms |
217116 KB |
Output is correct |
63 |
Correct |
165 ms |
211576 KB |
Output is correct |
64 |
Correct |
175 ms |
211536 KB |
Output is correct |
65 |
Correct |
193 ms |
212844 KB |
Output is correct |
66 |
Correct |
190 ms |
212744 KB |
Output is correct |
67 |
Correct |
157 ms |
211416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
154476 KB |
Output is correct |
2 |
Correct |
35 ms |
154460 KB |
Output is correct |
3 |
Correct |
35 ms |
154460 KB |
Output is correct |
4 |
Correct |
38 ms |
154456 KB |
Output is correct |
5 |
Correct |
36 ms |
154464 KB |
Output is correct |
6 |
Correct |
34 ms |
154460 KB |
Output is correct |
7 |
Correct |
33 ms |
154460 KB |
Output is correct |
8 |
Correct |
34 ms |
154456 KB |
Output is correct |
9 |
Correct |
33 ms |
154452 KB |
Output is correct |
10 |
Correct |
34 ms |
154460 KB |
Output is correct |
11 |
Correct |
34 ms |
154448 KB |
Output is correct |
12 |
Correct |
34 ms |
154456 KB |
Output is correct |
13 |
Correct |
729 ms |
329492 KB |
Output is correct |
14 |
Correct |
857 ms |
356088 KB |
Output is correct |
15 |
Correct |
1027 ms |
384448 KB |
Output is correct |
16 |
Correct |
1201 ms |
417884 KB |
Output is correct |
17 |
Correct |
1483 ms |
452372 KB |
Output is correct |
18 |
Correct |
1402 ms |
452388 KB |
Output is correct |
19 |
Correct |
1405 ms |
452460 KB |
Output is correct |
20 |
Correct |
33 ms |
154352 KB |
Output is correct |
21 |
Correct |
34 ms |
154452 KB |
Output is correct |
22 |
Correct |
34 ms |
154456 KB |
Output is correct |
23 |
Correct |
34 ms |
154484 KB |
Output is correct |
24 |
Correct |
34 ms |
154508 KB |
Output is correct |
25 |
Correct |
37 ms |
154568 KB |
Output is correct |
26 |
Correct |
34 ms |
154456 KB |
Output is correct |
27 |
Correct |
38 ms |
154448 KB |
Output is correct |
28 |
Correct |
34 ms |
154460 KB |
Output is correct |
29 |
Correct |
35 ms |
154460 KB |
Output is correct |
30 |
Correct |
33 ms |
154424 KB |
Output is correct |
31 |
Correct |
36 ms |
154488 KB |
Output is correct |
32 |
Correct |
34 ms |
154448 KB |
Output is correct |
33 |
Correct |
36 ms |
154452 KB |
Output is correct |
34 |
Correct |
34 ms |
154460 KB |
Output is correct |
35 |
Correct |
34 ms |
154448 KB |
Output is correct |
36 |
Correct |
34 ms |
154712 KB |
Output is correct |
37 |
Correct |
34 ms |
154460 KB |
Output is correct |
38 |
Correct |
34 ms |
154460 KB |
Output is correct |
39 |
Correct |
33 ms |
154448 KB |
Output is correct |
40 |
Correct |
34 ms |
154448 KB |
Output is correct |
41 |
Correct |
36 ms |
154460 KB |
Output is correct |
42 |
Correct |
34 ms |
154452 KB |
Output is correct |
43 |
Correct |
34 ms |
154460 KB |
Output is correct |
44 |
Correct |
37 ms |
154456 KB |
Output is correct |
45 |
Correct |
33 ms |
154596 KB |
Output is correct |
46 |
Correct |
34 ms |
154388 KB |
Output is correct |
47 |
Correct |
35 ms |
154712 KB |
Output is correct |
48 |
Correct |
34 ms |
154460 KB |
Output is correct |
49 |
Correct |
34 ms |
154380 KB |
Output is correct |
50 |
Correct |
34 ms |
154456 KB |
Output is correct |
51 |
Correct |
34 ms |
154716 KB |
Output is correct |
52 |
Correct |
35 ms |
154464 KB |
Output is correct |
53 |
Correct |
33 ms |
154468 KB |
Output is correct |
54 |
Correct |
35 ms |
154456 KB |
Output is correct |
55 |
Correct |
38 ms |
154496 KB |
Output is correct |
56 |
Correct |
34 ms |
154460 KB |
Output is correct |
57 |
Correct |
34 ms |
154716 KB |
Output is correct |
58 |
Correct |
41 ms |
162640 KB |
Output is correct |
59 |
Correct |
40 ms |
162644 KB |
Output is correct |
60 |
Correct |
43 ms |
162644 KB |
Output is correct |
61 |
Correct |
40 ms |
162652 KB |
Output is correct |
62 |
Correct |
40 ms |
162652 KB |
Output is correct |
63 |
Correct |
38 ms |
162608 KB |
Output is correct |
64 |
Correct |
40 ms |
162644 KB |
Output is correct |
65 |
Correct |
39 ms |
162412 KB |
Output is correct |
66 |
Correct |
42 ms |
162440 KB |
Output is correct |
67 |
Correct |
40 ms |
162640 KB |
Output is correct |
68 |
Correct |
39 ms |
162640 KB |
Output is correct |
69 |
Correct |
44 ms |
162644 KB |
Output is correct |
70 |
Correct |
39 ms |
162536 KB |
Output is correct |
71 |
Correct |
63 ms |
174164 KB |
Output is correct |
72 |
Correct |
225 ms |
219732 KB |
Output is correct |
73 |
Correct |
178 ms |
211736 KB |
Output is correct |
74 |
Correct |
171 ms |
211540 KB |
Output is correct |
75 |
Correct |
162 ms |
211540 KB |
Output is correct |
76 |
Correct |
160 ms |
211396 KB |
Output is correct |
77 |
Correct |
157 ms |
211828 KB |
Output is correct |
78 |
Correct |
157 ms |
211608 KB |
Output is correct |
79 |
Correct |
191 ms |
217116 KB |
Output is correct |
80 |
Correct |
165 ms |
211576 KB |
Output is correct |
81 |
Correct |
175 ms |
211536 KB |
Output is correct |
82 |
Correct |
193 ms |
212844 KB |
Output is correct |
83 |
Correct |
190 ms |
212744 KB |
Output is correct |
84 |
Correct |
157 ms |
211416 KB |
Output is correct |
85 |
Correct |
411 ms |
269320 KB |
Output is correct |
86 |
Correct |
989 ms |
387380 KB |
Output is correct |
87 |
Correct |
933 ms |
387168 KB |
Output is correct |
88 |
Correct |
888 ms |
387396 KB |
Output is correct |
89 |
Correct |
873 ms |
387264 KB |
Output is correct |
90 |
Correct |
871 ms |
387276 KB |
Output is correct |
91 |
Correct |
944 ms |
397780 KB |
Output is correct |
92 |
Correct |
1130 ms |
416732 KB |
Output is correct |
93 |
Correct |
899 ms |
387400 KB |
Output is correct |
94 |
Correct |
1029 ms |
387424 KB |
Output is correct |
95 |
Correct |
1258 ms |
404560 KB |
Output is correct |
96 |
Correct |
1236 ms |
404488 KB |
Output is correct |
97 |
Correct |
860 ms |
387208 KB |
Output is correct |