#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 MOD = 998244353;
const int MOD2 = 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;
}
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 len = 1 ; len <= n ; ++len)
{
std::map <int,int> map;
for (int pos = n - len + 1 ; pos >= 1 ; --pos)
{
int r = pos + len - 1;
next[pos][r] = map[getHash(pos, r)];
if (r + len - 1 <= n)
{
map[getHash(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:113:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
113 | std::cin >> s + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
148308 KB |
Output is correct |
2 |
Correct |
46 ms |
148308 KB |
Output is correct |
3 |
Correct |
43 ms |
148304 KB |
Output is correct |
4 |
Correct |
44 ms |
148308 KB |
Output is correct |
5 |
Correct |
43 ms |
148312 KB |
Output is correct |
6 |
Correct |
42 ms |
148312 KB |
Output is correct |
7 |
Correct |
46 ms |
148472 KB |
Output is correct |
8 |
Correct |
44 ms |
148476 KB |
Output is correct |
9 |
Correct |
43 ms |
148312 KB |
Output is correct |
10 |
Correct |
42 ms |
148312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
148560 KB |
Output is correct |
2 |
Correct |
42 ms |
148304 KB |
Output is correct |
3 |
Correct |
760 ms |
308084 KB |
Output is correct |
4 |
Correct |
895 ms |
332692 KB |
Output is correct |
5 |
Correct |
1069 ms |
361788 KB |
Output is correct |
6 |
Correct |
1248 ms |
394320 KB |
Output is correct |
7 |
Correct |
1501 ms |
432044 KB |
Output is correct |
8 |
Correct |
1490 ms |
432420 KB |
Output is correct |
9 |
Correct |
1502 ms |
432272 KB |
Output is correct |
10 |
Correct |
43 ms |
148308 KB |
Output is correct |
11 |
Correct |
45 ms |
148568 KB |
Output is correct |
12 |
Correct |
43 ms |
148308 KB |
Output is correct |
13 |
Correct |
47 ms |
148504 KB |
Output is correct |
14 |
Correct |
42 ms |
148412 KB |
Output is correct |
15 |
Correct |
43 ms |
148612 KB |
Output is correct |
16 |
Correct |
42 ms |
148828 KB |
Output is correct |
17 |
Correct |
42 ms |
148308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
148308 KB |
Output is correct |
2 |
Correct |
46 ms |
148308 KB |
Output is correct |
3 |
Correct |
43 ms |
148304 KB |
Output is correct |
4 |
Correct |
44 ms |
148308 KB |
Output is correct |
5 |
Correct |
43 ms |
148312 KB |
Output is correct |
6 |
Correct |
42 ms |
148312 KB |
Output is correct |
7 |
Correct |
46 ms |
148472 KB |
Output is correct |
8 |
Correct |
44 ms |
148476 KB |
Output is correct |
9 |
Correct |
43 ms |
148312 KB |
Output is correct |
10 |
Correct |
42 ms |
148312 KB |
Output is correct |
11 |
Correct |
42 ms |
148572 KB |
Output is correct |
12 |
Correct |
45 ms |
148568 KB |
Output is correct |
13 |
Correct |
48 ms |
149072 KB |
Output is correct |
14 |
Correct |
46 ms |
148304 KB |
Output is correct |
15 |
Correct |
43 ms |
148560 KB |
Output is correct |
16 |
Correct |
43 ms |
148620 KB |
Output is correct |
17 |
Correct |
42 ms |
148308 KB |
Output is correct |
18 |
Correct |
47 ms |
148508 KB |
Output is correct |
19 |
Correct |
46 ms |
148640 KB |
Output is correct |
20 |
Correct |
43 ms |
148304 KB |
Output is correct |
21 |
Correct |
44 ms |
148712 KB |
Output is correct |
22 |
Correct |
42 ms |
148932 KB |
Output is correct |
23 |
Correct |
43 ms |
148828 KB |
Output is correct |
24 |
Correct |
43 ms |
148820 KB |
Output is correct |
25 |
Correct |
43 ms |
148816 KB |
Output is correct |
26 |
Correct |
45 ms |
148820 KB |
Output is correct |
27 |
Correct |
45 ms |
148828 KB |
Output is correct |
28 |
Correct |
42 ms |
148816 KB |
Output is correct |
29 |
Correct |
44 ms |
148824 KB |
Output is correct |
30 |
Correct |
45 ms |
148828 KB |
Output is correct |
31 |
Correct |
43 ms |
148816 KB |
Output is correct |
32 |
Correct |
43 ms |
148816 KB |
Output is correct |
33 |
Correct |
43 ms |
149156 KB |
Output is correct |
34 |
Correct |
43 ms |
148228 KB |
Output is correct |
35 |
Correct |
43 ms |
148308 KB |
Output is correct |
36 |
Correct |
42 ms |
148280 KB |
Output is correct |
37 |
Correct |
42 ms |
148552 KB |
Output is correct |
38 |
Correct |
42 ms |
148564 KB |
Output is correct |
39 |
Correct |
44 ms |
148932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
148308 KB |
Output is correct |
2 |
Correct |
46 ms |
148308 KB |
Output is correct |
3 |
Correct |
43 ms |
148304 KB |
Output is correct |
4 |
Correct |
44 ms |
148308 KB |
Output is correct |
5 |
Correct |
43 ms |
148312 KB |
Output is correct |
6 |
Correct |
42 ms |
148312 KB |
Output is correct |
7 |
Correct |
46 ms |
148472 KB |
Output is correct |
8 |
Correct |
44 ms |
148476 KB |
Output is correct |
9 |
Correct |
43 ms |
148312 KB |
Output is correct |
10 |
Correct |
42 ms |
148312 KB |
Output is correct |
11 |
Correct |
42 ms |
148572 KB |
Output is correct |
12 |
Correct |
45 ms |
148568 KB |
Output is correct |
13 |
Correct |
48 ms |
149072 KB |
Output is correct |
14 |
Correct |
46 ms |
148304 KB |
Output is correct |
15 |
Correct |
43 ms |
148560 KB |
Output is correct |
16 |
Correct |
43 ms |
148620 KB |
Output is correct |
17 |
Correct |
42 ms |
148308 KB |
Output is correct |
18 |
Correct |
47 ms |
148508 KB |
Output is correct |
19 |
Correct |
46 ms |
148640 KB |
Output is correct |
20 |
Correct |
43 ms |
148304 KB |
Output is correct |
21 |
Correct |
44 ms |
148712 KB |
Output is correct |
22 |
Correct |
42 ms |
148932 KB |
Output is correct |
23 |
Correct |
43 ms |
148828 KB |
Output is correct |
24 |
Correct |
43 ms |
148820 KB |
Output is correct |
25 |
Correct |
43 ms |
148816 KB |
Output is correct |
26 |
Correct |
45 ms |
148820 KB |
Output is correct |
27 |
Correct |
45 ms |
148828 KB |
Output is correct |
28 |
Correct |
42 ms |
148816 KB |
Output is correct |
29 |
Correct |
44 ms |
148824 KB |
Output is correct |
30 |
Correct |
45 ms |
148828 KB |
Output is correct |
31 |
Correct |
43 ms |
148816 KB |
Output is correct |
32 |
Correct |
43 ms |
148816 KB |
Output is correct |
33 |
Correct |
43 ms |
149156 KB |
Output is correct |
34 |
Correct |
43 ms |
148228 KB |
Output is correct |
35 |
Correct |
43 ms |
148308 KB |
Output is correct |
36 |
Correct |
42 ms |
148280 KB |
Output is correct |
37 |
Correct |
42 ms |
148552 KB |
Output is correct |
38 |
Correct |
42 ms |
148564 KB |
Output is correct |
39 |
Correct |
44 ms |
148932 KB |
Output is correct |
40 |
Correct |
44 ms |
149844 KB |
Output is correct |
41 |
Correct |
50 ms |
152996 KB |
Output is correct |
42 |
Correct |
49 ms |
152704 KB |
Output is correct |
43 |
Correct |
50 ms |
152808 KB |
Output is correct |
44 |
Correct |
51 ms |
152752 KB |
Output is correct |
45 |
Correct |
48 ms |
152668 KB |
Output is correct |
46 |
Correct |
48 ms |
152660 KB |
Output is correct |
47 |
Correct |
47 ms |
152668 KB |
Output is correct |
48 |
Correct |
49 ms |
152808 KB |
Output is correct |
49 |
Correct |
48 ms |
152656 KB |
Output is correct |
50 |
Correct |
48 ms |
152656 KB |
Output is correct |
51 |
Correct |
51 ms |
152660 KB |
Output is correct |
52 |
Correct |
48 ms |
152656 KB |
Output is correct |
53 |
Correct |
48 ms |
152660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
148308 KB |
Output is correct |
2 |
Correct |
46 ms |
148308 KB |
Output is correct |
3 |
Correct |
43 ms |
148304 KB |
Output is correct |
4 |
Correct |
44 ms |
148308 KB |
Output is correct |
5 |
Correct |
43 ms |
148312 KB |
Output is correct |
6 |
Correct |
42 ms |
148312 KB |
Output is correct |
7 |
Correct |
46 ms |
148472 KB |
Output is correct |
8 |
Correct |
44 ms |
148476 KB |
Output is correct |
9 |
Correct |
43 ms |
148312 KB |
Output is correct |
10 |
Correct |
42 ms |
148312 KB |
Output is correct |
11 |
Correct |
42 ms |
148572 KB |
Output is correct |
12 |
Correct |
45 ms |
148568 KB |
Output is correct |
13 |
Correct |
48 ms |
149072 KB |
Output is correct |
14 |
Correct |
46 ms |
148304 KB |
Output is correct |
15 |
Correct |
43 ms |
148560 KB |
Output is correct |
16 |
Correct |
43 ms |
148620 KB |
Output is correct |
17 |
Correct |
42 ms |
148308 KB |
Output is correct |
18 |
Correct |
47 ms |
148508 KB |
Output is correct |
19 |
Correct |
46 ms |
148640 KB |
Output is correct |
20 |
Correct |
43 ms |
148304 KB |
Output is correct |
21 |
Correct |
44 ms |
148712 KB |
Output is correct |
22 |
Correct |
42 ms |
148932 KB |
Output is correct |
23 |
Correct |
43 ms |
148828 KB |
Output is correct |
24 |
Correct |
43 ms |
148820 KB |
Output is correct |
25 |
Correct |
43 ms |
148816 KB |
Output is correct |
26 |
Correct |
45 ms |
148820 KB |
Output is correct |
27 |
Correct |
45 ms |
148828 KB |
Output is correct |
28 |
Correct |
42 ms |
148816 KB |
Output is correct |
29 |
Correct |
44 ms |
148824 KB |
Output is correct |
30 |
Correct |
45 ms |
148828 KB |
Output is correct |
31 |
Correct |
43 ms |
148816 KB |
Output is correct |
32 |
Correct |
43 ms |
148816 KB |
Output is correct |
33 |
Correct |
43 ms |
149156 KB |
Output is correct |
34 |
Correct |
43 ms |
148228 KB |
Output is correct |
35 |
Correct |
43 ms |
148308 KB |
Output is correct |
36 |
Correct |
42 ms |
148280 KB |
Output is correct |
37 |
Correct |
42 ms |
148552 KB |
Output is correct |
38 |
Correct |
42 ms |
148564 KB |
Output is correct |
39 |
Correct |
44 ms |
148932 KB |
Output is correct |
40 |
Correct |
44 ms |
149844 KB |
Output is correct |
41 |
Correct |
50 ms |
152996 KB |
Output is correct |
42 |
Correct |
49 ms |
152704 KB |
Output is correct |
43 |
Correct |
50 ms |
152808 KB |
Output is correct |
44 |
Correct |
51 ms |
152752 KB |
Output is correct |
45 |
Correct |
48 ms |
152668 KB |
Output is correct |
46 |
Correct |
48 ms |
152660 KB |
Output is correct |
47 |
Correct |
47 ms |
152668 KB |
Output is correct |
48 |
Correct |
49 ms |
152808 KB |
Output is correct |
49 |
Correct |
48 ms |
152656 KB |
Output is correct |
50 |
Correct |
48 ms |
152656 KB |
Output is correct |
51 |
Correct |
51 ms |
152660 KB |
Output is correct |
52 |
Correct |
48 ms |
152656 KB |
Output is correct |
53 |
Correct |
48 ms |
152660 KB |
Output is correct |
54 |
Correct |
69 ms |
161360 KB |
Output is correct |
55 |
Correct |
228 ms |
202064 KB |
Output is correct |
56 |
Correct |
187 ms |
193748 KB |
Output is correct |
57 |
Correct |
173 ms |
193688 KB |
Output is correct |
58 |
Correct |
167 ms |
193896 KB |
Output is correct |
59 |
Correct |
176 ms |
193980 KB |
Output is correct |
60 |
Correct |
166 ms |
197920 KB |
Output is correct |
61 |
Correct |
186 ms |
193872 KB |
Output is correct |
62 |
Correct |
215 ms |
199944 KB |
Output is correct |
63 |
Correct |
177 ms |
193836 KB |
Output is correct |
64 |
Correct |
221 ms |
193716 KB |
Output is correct |
65 |
Correct |
211 ms |
200348 KB |
Output is correct |
66 |
Correct |
234 ms |
207572 KB |
Output is correct |
67 |
Correct |
165 ms |
211536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
148308 KB |
Output is correct |
2 |
Correct |
46 ms |
148308 KB |
Output is correct |
3 |
Correct |
43 ms |
148304 KB |
Output is correct |
4 |
Correct |
44 ms |
148308 KB |
Output is correct |
5 |
Correct |
43 ms |
148312 KB |
Output is correct |
6 |
Correct |
42 ms |
148312 KB |
Output is correct |
7 |
Correct |
46 ms |
148472 KB |
Output is correct |
8 |
Correct |
44 ms |
148476 KB |
Output is correct |
9 |
Correct |
43 ms |
148312 KB |
Output is correct |
10 |
Correct |
42 ms |
148312 KB |
Output is correct |
11 |
Correct |
45 ms |
148560 KB |
Output is correct |
12 |
Correct |
42 ms |
148304 KB |
Output is correct |
13 |
Correct |
760 ms |
308084 KB |
Output is correct |
14 |
Correct |
895 ms |
332692 KB |
Output is correct |
15 |
Correct |
1069 ms |
361788 KB |
Output is correct |
16 |
Correct |
1248 ms |
394320 KB |
Output is correct |
17 |
Correct |
1501 ms |
432044 KB |
Output is correct |
18 |
Correct |
1490 ms |
432420 KB |
Output is correct |
19 |
Correct |
1502 ms |
432272 KB |
Output is correct |
20 |
Correct |
43 ms |
148308 KB |
Output is correct |
21 |
Correct |
45 ms |
148568 KB |
Output is correct |
22 |
Correct |
43 ms |
148308 KB |
Output is correct |
23 |
Correct |
47 ms |
148504 KB |
Output is correct |
24 |
Correct |
42 ms |
148412 KB |
Output is correct |
25 |
Correct |
43 ms |
148612 KB |
Output is correct |
26 |
Correct |
42 ms |
148828 KB |
Output is correct |
27 |
Correct |
42 ms |
148308 KB |
Output is correct |
28 |
Correct |
42 ms |
148572 KB |
Output is correct |
29 |
Correct |
45 ms |
148568 KB |
Output is correct |
30 |
Correct |
48 ms |
149072 KB |
Output is correct |
31 |
Correct |
46 ms |
148304 KB |
Output is correct |
32 |
Correct |
43 ms |
148560 KB |
Output is correct |
33 |
Correct |
43 ms |
148620 KB |
Output is correct |
34 |
Correct |
42 ms |
148308 KB |
Output is correct |
35 |
Correct |
47 ms |
148508 KB |
Output is correct |
36 |
Correct |
46 ms |
148640 KB |
Output is correct |
37 |
Correct |
43 ms |
148304 KB |
Output is correct |
38 |
Correct |
44 ms |
148712 KB |
Output is correct |
39 |
Correct |
42 ms |
148932 KB |
Output is correct |
40 |
Correct |
43 ms |
148828 KB |
Output is correct |
41 |
Correct |
43 ms |
148820 KB |
Output is correct |
42 |
Correct |
43 ms |
148816 KB |
Output is correct |
43 |
Correct |
45 ms |
148820 KB |
Output is correct |
44 |
Correct |
45 ms |
148828 KB |
Output is correct |
45 |
Correct |
42 ms |
148816 KB |
Output is correct |
46 |
Correct |
44 ms |
148824 KB |
Output is correct |
47 |
Correct |
45 ms |
148828 KB |
Output is correct |
48 |
Correct |
43 ms |
148816 KB |
Output is correct |
49 |
Correct |
43 ms |
148816 KB |
Output is correct |
50 |
Correct |
43 ms |
149156 KB |
Output is correct |
51 |
Correct |
43 ms |
148228 KB |
Output is correct |
52 |
Correct |
43 ms |
148308 KB |
Output is correct |
53 |
Correct |
42 ms |
148280 KB |
Output is correct |
54 |
Correct |
42 ms |
148552 KB |
Output is correct |
55 |
Correct |
42 ms |
148564 KB |
Output is correct |
56 |
Correct |
44 ms |
148932 KB |
Output is correct |
57 |
Correct |
44 ms |
149844 KB |
Output is correct |
58 |
Correct |
50 ms |
152996 KB |
Output is correct |
59 |
Correct |
49 ms |
152704 KB |
Output is correct |
60 |
Correct |
50 ms |
152808 KB |
Output is correct |
61 |
Correct |
51 ms |
152752 KB |
Output is correct |
62 |
Correct |
48 ms |
152668 KB |
Output is correct |
63 |
Correct |
48 ms |
152660 KB |
Output is correct |
64 |
Correct |
47 ms |
152668 KB |
Output is correct |
65 |
Correct |
49 ms |
152808 KB |
Output is correct |
66 |
Correct |
48 ms |
152656 KB |
Output is correct |
67 |
Correct |
48 ms |
152656 KB |
Output is correct |
68 |
Correct |
51 ms |
152660 KB |
Output is correct |
69 |
Correct |
48 ms |
152656 KB |
Output is correct |
70 |
Correct |
48 ms |
152660 KB |
Output is correct |
71 |
Correct |
69 ms |
161360 KB |
Output is correct |
72 |
Correct |
228 ms |
202064 KB |
Output is correct |
73 |
Correct |
187 ms |
193748 KB |
Output is correct |
74 |
Correct |
173 ms |
193688 KB |
Output is correct |
75 |
Correct |
167 ms |
193896 KB |
Output is correct |
76 |
Correct |
176 ms |
193980 KB |
Output is correct |
77 |
Correct |
166 ms |
197920 KB |
Output is correct |
78 |
Correct |
186 ms |
193872 KB |
Output is correct |
79 |
Correct |
215 ms |
199944 KB |
Output is correct |
80 |
Correct |
177 ms |
193836 KB |
Output is correct |
81 |
Correct |
221 ms |
193716 KB |
Output is correct |
82 |
Correct |
211 ms |
200348 KB |
Output is correct |
83 |
Correct |
234 ms |
207572 KB |
Output is correct |
84 |
Correct |
165 ms |
211536 KB |
Output is correct |
85 |
Correct |
441 ms |
269388 KB |
Output is correct |
86 |
Correct |
975 ms |
387388 KB |
Output is correct |
87 |
Correct |
932 ms |
387380 KB |
Output is correct |
88 |
Correct |
969 ms |
387268 KB |
Output is correct |
89 |
Incorrect |
838 ms |
387292 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |