/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2510;
const ll base = 29, inf = 1e18;
int n;
ll a, b, c, h[maxn], pw[maxn];
string s;
vector < int > occ[maxn * maxn];
ll hs_dp[maxn * maxn], pt[maxn * maxn];
unordered_map < ll, int > nxt[maxn];
ll range_hash[maxn][maxn];
ll get_hash(int i, int j)
{
return range_hash[i][j];
}
unordered_map < ll, ll > rev;
void solve()
{
cin >> n >> s >> a >> b >> c;
s = "#" + s;
pw[0] = 1;
for (int i = 1; i <= n; i ++)
{
h[i] = h[i - 1] * base + (s[i] - 'a' + 1);
pw[i] = pw[i - 1] * base;
}
vector < ll > cp;
for (int i = 1; i <= n; i ++)
for (int j = i; j <= n; j ++)
{
range_hash[i][j] = h[j] - h[i - 1] * pw[j - i + 1];
cp.push_back(range_hash[i][j]);
}
sort(cp.begin(), cp.end());
int cnt = 0;
for (int i = 0; i < cp.size(); i ++)
{
if (i == 0 || cp[i] != cp[i - 1])
rev[cp[i]] = ++ cnt;
}
for (int i = 1; i <= n; i ++)
for (int j = i; j <= n; j ++)
range_hash[i][j] = rev[range_hash[i][j]];
for (int i = n; i > 0; i --)
{
for (int p = i; p <= n; p ++)
{
ll cur_hs = get_hash(i, p);
occ[cur_hs].push_back(i);
/**cout << "hash " << i << " " << p << endl;
cout << "occurrences: ";
for (int ds : occ[cur_hs])
cout << ds << " ";
cout << endl;*/
if (occ[cur_hs].size() == 1)
{
hs_dp[cur_hs] = hs_dp[get_hash(i + 1, p)] + a;
nxt[p][cur_hs] = n + 1;
pt[cur_hs] = -1;
}
else
{
hs_dp[cur_hs] = min(hs_dp[cur_hs], hs_dp[get_hash(i + 1, p)] + a);
if (pt[cur_hs] == -1)
{
pt[cur_hs] ++;
}
while(occ[cur_hs][pt[cur_hs]] > p)
pt[cur_hs] ++;
pt[cur_hs] --;
if (pt[cur_hs] == -1)
nxt[p][cur_hs] = n + 1;
else
nxt[p][cur_hs] = occ[cur_hs][pt[cur_hs]] + (p - i);
///cout << "step " << i << " " << p << " " << nxt[p][cur_hs] << endl;
}
}
for (int k = i; k <= n; k ++)
{
ll clip_hash, clip_len = k - i + 1;
clip_hash = get_hash(i, k);
ll cost = hs_dp[clip_hash] + b + c;
if (occ[get_hash(i, k)].size() < 2)
break;
int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
///dp[get_hash(i, k)] = min(dp[get_hash(i, k)], cos);
///cout << "-------------------" << endl;
///cout << i << " " << k << endl;
while(pos <= n)
{
///cout << pos << " " << cost << endl;
int to = nxt[pos][clip_hash];
if (to > n)
break;
cost = cost + (to - pos - clip_len) * a;
cost = cost + c;
///cout << "stop " << occ[clip_hash][pt] << endl;
///cost = cost + (occ[clip_hash][pt] - pos) * a;
///cost = cost + c;
pos = to;
hs_dp[get_hash(i, pos)] = min(hs_dp[get_hash(i, pos)], cost);
/**int to = pos + clip_len - 1;
if (to > n)
{
break;
}
if (get_hash(pos, to) == clip_hash)
{
cost = cost + c;
pos = pos + clip_len;
hs_dp[get_hash(i, to)] = min(hs_dp[get_hash(i, to)], cost);
}
else
{
pos ++;
cost += a;
}*/
}
}
for (int k = i; k <= n; k ++)
{
hs_dp[get_hash(i, k)] = min(hs_dp[get_hash(i, k)], hs_dp[get_hash(i, k - 1)] + a);
}
}
/**for (int i = 1; i <= n; i ++, cout << endl)
for (int j = i; j <= n; j ++)
cout << hs_dp[get_hash(i, j)] << " ";*/
cout << hs_dp[get_hash(1, n)] << endl;
}
int main()
{
solve();
return 0;
}
Compilation message
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < cp.size(); i ++)
| ~~^~~~~~~~~~~
copypaste3.cpp:114:26: warning: unused variable 'pt' [-Wunused-variable]
114 | int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148300 KB |
Output is correct |
2 |
Correct |
63 ms |
148400 KB |
Output is correct |
3 |
Correct |
63 ms |
148384 KB |
Output is correct |
4 |
Correct |
63 ms |
148420 KB |
Output is correct |
5 |
Correct |
63 ms |
148412 KB |
Output is correct |
6 |
Correct |
63 ms |
148408 KB |
Output is correct |
7 |
Correct |
63 ms |
148316 KB |
Output is correct |
8 |
Correct |
64 ms |
148296 KB |
Output is correct |
9 |
Correct |
64 ms |
148348 KB |
Output is correct |
10 |
Correct |
65 ms |
148300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148484 KB |
Output is correct |
2 |
Correct |
63 ms |
148300 KB |
Output is correct |
3 |
Correct |
2211 ms |
263456 KB |
Output is correct |
4 |
Correct |
2769 ms |
281268 KB |
Output is correct |
5 |
Execution timed out |
3075 ms |
293492 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148300 KB |
Output is correct |
2 |
Correct |
63 ms |
148400 KB |
Output is correct |
3 |
Correct |
63 ms |
148384 KB |
Output is correct |
4 |
Correct |
63 ms |
148420 KB |
Output is correct |
5 |
Correct |
63 ms |
148412 KB |
Output is correct |
6 |
Correct |
63 ms |
148408 KB |
Output is correct |
7 |
Correct |
63 ms |
148316 KB |
Output is correct |
8 |
Correct |
64 ms |
148296 KB |
Output is correct |
9 |
Correct |
64 ms |
148348 KB |
Output is correct |
10 |
Correct |
65 ms |
148300 KB |
Output is correct |
11 |
Correct |
63 ms |
148384 KB |
Output is correct |
12 |
Correct |
64 ms |
148448 KB |
Output is correct |
13 |
Correct |
66 ms |
148428 KB |
Output is correct |
14 |
Correct |
62 ms |
148456 KB |
Output is correct |
15 |
Correct |
63 ms |
148432 KB |
Output is correct |
16 |
Correct |
63 ms |
148356 KB |
Output is correct |
17 |
Correct |
64 ms |
148336 KB |
Output is correct |
18 |
Correct |
64 ms |
148412 KB |
Output is correct |
19 |
Correct |
65 ms |
148392 KB |
Output is correct |
20 |
Correct |
62 ms |
148396 KB |
Output is correct |
21 |
Correct |
65 ms |
148552 KB |
Output is correct |
22 |
Correct |
63 ms |
148564 KB |
Output is correct |
23 |
Correct |
73 ms |
148512 KB |
Output is correct |
24 |
Correct |
66 ms |
148556 KB |
Output is correct |
25 |
Correct |
65 ms |
148544 KB |
Output is correct |
26 |
Correct |
64 ms |
148544 KB |
Output is correct |
27 |
Correct |
64 ms |
148588 KB |
Output is correct |
28 |
Correct |
65 ms |
148464 KB |
Output is correct |
29 |
Correct |
65 ms |
148596 KB |
Output is correct |
30 |
Correct |
66 ms |
148568 KB |
Output is correct |
31 |
Correct |
63 ms |
148560 KB |
Output is correct |
32 |
Correct |
64 ms |
148504 KB |
Output is correct |
33 |
Correct |
64 ms |
148584 KB |
Output is correct |
34 |
Correct |
63 ms |
148408 KB |
Output is correct |
35 |
Correct |
65 ms |
148384 KB |
Output is correct |
36 |
Correct |
65 ms |
148328 KB |
Output is correct |
37 |
Correct |
64 ms |
148456 KB |
Output is correct |
38 |
Correct |
66 ms |
148456 KB |
Output is correct |
39 |
Correct |
65 ms |
148568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148300 KB |
Output is correct |
2 |
Correct |
63 ms |
148400 KB |
Output is correct |
3 |
Correct |
63 ms |
148384 KB |
Output is correct |
4 |
Correct |
63 ms |
148420 KB |
Output is correct |
5 |
Correct |
63 ms |
148412 KB |
Output is correct |
6 |
Correct |
63 ms |
148408 KB |
Output is correct |
7 |
Correct |
63 ms |
148316 KB |
Output is correct |
8 |
Correct |
64 ms |
148296 KB |
Output is correct |
9 |
Correct |
64 ms |
148348 KB |
Output is correct |
10 |
Correct |
65 ms |
148300 KB |
Output is correct |
11 |
Correct |
63 ms |
148384 KB |
Output is correct |
12 |
Correct |
64 ms |
148448 KB |
Output is correct |
13 |
Correct |
66 ms |
148428 KB |
Output is correct |
14 |
Correct |
62 ms |
148456 KB |
Output is correct |
15 |
Correct |
63 ms |
148432 KB |
Output is correct |
16 |
Correct |
63 ms |
148356 KB |
Output is correct |
17 |
Correct |
64 ms |
148336 KB |
Output is correct |
18 |
Correct |
64 ms |
148412 KB |
Output is correct |
19 |
Correct |
65 ms |
148392 KB |
Output is correct |
20 |
Correct |
62 ms |
148396 KB |
Output is correct |
21 |
Correct |
65 ms |
148552 KB |
Output is correct |
22 |
Correct |
63 ms |
148564 KB |
Output is correct |
23 |
Correct |
73 ms |
148512 KB |
Output is correct |
24 |
Correct |
66 ms |
148556 KB |
Output is correct |
25 |
Correct |
65 ms |
148544 KB |
Output is correct |
26 |
Correct |
64 ms |
148544 KB |
Output is correct |
27 |
Correct |
64 ms |
148588 KB |
Output is correct |
28 |
Correct |
65 ms |
148464 KB |
Output is correct |
29 |
Correct |
65 ms |
148596 KB |
Output is correct |
30 |
Correct |
66 ms |
148568 KB |
Output is correct |
31 |
Correct |
63 ms |
148560 KB |
Output is correct |
32 |
Correct |
64 ms |
148504 KB |
Output is correct |
33 |
Correct |
64 ms |
148584 KB |
Output is correct |
34 |
Correct |
63 ms |
148408 KB |
Output is correct |
35 |
Correct |
65 ms |
148384 KB |
Output is correct |
36 |
Correct |
65 ms |
148328 KB |
Output is correct |
37 |
Correct |
64 ms |
148456 KB |
Output is correct |
38 |
Correct |
66 ms |
148456 KB |
Output is correct |
39 |
Correct |
65 ms |
148568 KB |
Output is correct |
40 |
Correct |
65 ms |
149196 KB |
Output is correct |
41 |
Correct |
71 ms |
150436 KB |
Output is correct |
42 |
Correct |
72 ms |
151956 KB |
Output is correct |
43 |
Correct |
72 ms |
152092 KB |
Output is correct |
44 |
Correct |
73 ms |
152084 KB |
Output is correct |
45 |
Correct |
73 ms |
152128 KB |
Output is correct |
46 |
Correct |
72 ms |
152088 KB |
Output is correct |
47 |
Correct |
70 ms |
150728 KB |
Output is correct |
48 |
Correct |
71 ms |
151448 KB |
Output is correct |
49 |
Correct |
72 ms |
151604 KB |
Output is correct |
50 |
Correct |
79 ms |
151752 KB |
Output is correct |
51 |
Correct |
73 ms |
151368 KB |
Output is correct |
52 |
Correct |
73 ms |
151336 KB |
Output is correct |
53 |
Correct |
79 ms |
152104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148300 KB |
Output is correct |
2 |
Correct |
63 ms |
148400 KB |
Output is correct |
3 |
Correct |
63 ms |
148384 KB |
Output is correct |
4 |
Correct |
63 ms |
148420 KB |
Output is correct |
5 |
Correct |
63 ms |
148412 KB |
Output is correct |
6 |
Correct |
63 ms |
148408 KB |
Output is correct |
7 |
Correct |
63 ms |
148316 KB |
Output is correct |
8 |
Correct |
64 ms |
148296 KB |
Output is correct |
9 |
Correct |
64 ms |
148348 KB |
Output is correct |
10 |
Correct |
65 ms |
148300 KB |
Output is correct |
11 |
Correct |
63 ms |
148384 KB |
Output is correct |
12 |
Correct |
64 ms |
148448 KB |
Output is correct |
13 |
Correct |
66 ms |
148428 KB |
Output is correct |
14 |
Correct |
62 ms |
148456 KB |
Output is correct |
15 |
Correct |
63 ms |
148432 KB |
Output is correct |
16 |
Correct |
63 ms |
148356 KB |
Output is correct |
17 |
Correct |
64 ms |
148336 KB |
Output is correct |
18 |
Correct |
64 ms |
148412 KB |
Output is correct |
19 |
Correct |
65 ms |
148392 KB |
Output is correct |
20 |
Correct |
62 ms |
148396 KB |
Output is correct |
21 |
Correct |
65 ms |
148552 KB |
Output is correct |
22 |
Correct |
63 ms |
148564 KB |
Output is correct |
23 |
Correct |
73 ms |
148512 KB |
Output is correct |
24 |
Correct |
66 ms |
148556 KB |
Output is correct |
25 |
Correct |
65 ms |
148544 KB |
Output is correct |
26 |
Correct |
64 ms |
148544 KB |
Output is correct |
27 |
Correct |
64 ms |
148588 KB |
Output is correct |
28 |
Correct |
65 ms |
148464 KB |
Output is correct |
29 |
Correct |
65 ms |
148596 KB |
Output is correct |
30 |
Correct |
66 ms |
148568 KB |
Output is correct |
31 |
Correct |
63 ms |
148560 KB |
Output is correct |
32 |
Correct |
64 ms |
148504 KB |
Output is correct |
33 |
Correct |
64 ms |
148584 KB |
Output is correct |
34 |
Correct |
63 ms |
148408 KB |
Output is correct |
35 |
Correct |
65 ms |
148384 KB |
Output is correct |
36 |
Correct |
65 ms |
148328 KB |
Output is correct |
37 |
Correct |
64 ms |
148456 KB |
Output is correct |
38 |
Correct |
66 ms |
148456 KB |
Output is correct |
39 |
Correct |
65 ms |
148568 KB |
Output is correct |
40 |
Correct |
65 ms |
149196 KB |
Output is correct |
41 |
Correct |
71 ms |
150436 KB |
Output is correct |
42 |
Correct |
72 ms |
151956 KB |
Output is correct |
43 |
Correct |
72 ms |
152092 KB |
Output is correct |
44 |
Correct |
73 ms |
152084 KB |
Output is correct |
45 |
Correct |
73 ms |
152128 KB |
Output is correct |
46 |
Correct |
72 ms |
152088 KB |
Output is correct |
47 |
Correct |
70 ms |
150728 KB |
Output is correct |
48 |
Correct |
71 ms |
151448 KB |
Output is correct |
49 |
Correct |
72 ms |
151604 KB |
Output is correct |
50 |
Correct |
79 ms |
151752 KB |
Output is correct |
51 |
Correct |
73 ms |
151368 KB |
Output is correct |
52 |
Correct |
73 ms |
151336 KB |
Output is correct |
53 |
Correct |
79 ms |
152104 KB |
Output is correct |
54 |
Correct |
125 ms |
164908 KB |
Output is correct |
55 |
Correct |
349 ms |
184124 KB |
Output is correct |
56 |
Correct |
564 ms |
225516 KB |
Output is correct |
57 |
Correct |
540 ms |
225856 KB |
Output is correct |
58 |
Correct |
552 ms |
226020 KB |
Output is correct |
59 |
Correct |
599 ms |
226028 KB |
Output is correct |
60 |
Correct |
535 ms |
226184 KB |
Output is correct |
61 |
Correct |
419 ms |
200988 KB |
Output is correct |
62 |
Correct |
421 ms |
184616 KB |
Output is correct |
63 |
Correct |
560 ms |
216408 KB |
Output is correct |
64 |
Correct |
524 ms |
216924 KB |
Output is correct |
65 |
Correct |
515 ms |
203892 KB |
Output is correct |
66 |
Correct |
498 ms |
204016 KB |
Output is correct |
67 |
Correct |
592 ms |
226056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
148300 KB |
Output is correct |
2 |
Correct |
63 ms |
148400 KB |
Output is correct |
3 |
Correct |
63 ms |
148384 KB |
Output is correct |
4 |
Correct |
63 ms |
148420 KB |
Output is correct |
5 |
Correct |
63 ms |
148412 KB |
Output is correct |
6 |
Correct |
63 ms |
148408 KB |
Output is correct |
7 |
Correct |
63 ms |
148316 KB |
Output is correct |
8 |
Correct |
64 ms |
148296 KB |
Output is correct |
9 |
Correct |
64 ms |
148348 KB |
Output is correct |
10 |
Correct |
65 ms |
148300 KB |
Output is correct |
11 |
Correct |
62 ms |
148484 KB |
Output is correct |
12 |
Correct |
63 ms |
148300 KB |
Output is correct |
13 |
Correct |
2211 ms |
263456 KB |
Output is correct |
14 |
Correct |
2769 ms |
281268 KB |
Output is correct |
15 |
Execution timed out |
3075 ms |
293492 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |