#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first
#define vs second
const int mod = 998244353;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int P = 71;
int convert(char c){
return (c-'a') + 1;
}
void solve(){
int n;
cin >> n;
string s;
cin >> s;
int a , b , c;
cin >> a >> b >> c;
ll dp[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
dp[i][j] = 1e18;
}
}
map<pair<int,int> , int> m;
vector<int> v[n*n];
int cur = 0;
int hash_val[n][n] , hash_pos[n][n];
for(int i = 0; i < n; i++){
ll pow = 1;
ll hash = 0;
for(int j = i; j < n; j++){
hash += convert(s[j]) * pow;
pow *= P;
hash %= mod;
pow %= mod;
hash_val[i][j] = hash;
if(m.find(mp(hash,j-i+1)) == m.end()){
m[mp(hash,j-i+1)] = cur++;
}
hash_pos[i][j] = m[mp(hash,j-i+1)];
v[hash_pos[i][j]].pb(i);
}
}
for(int i = 0; i < cur; i++){
sort(all(v[i]));
}
int next[n][n];
memset(next,-1,sizeof next);
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
int pos = upper_bound(all(v[hash_pos[i][j]]),j) - v[hash_pos[i][j]].begin();
if(pos < v[hash_pos[i][j]].size()){
next[i][j] = v[hash_pos[i][j]][pos];
}
}
}
for(int len = 0; len < n; len++){
for(int i = 0; i + len < n; i++){
int j = i + len;
dp[i][j] = min(dp[i][j],(len+1LL)*a);
if(len > 0){
dp[i][j] = min(dp[i][j] , min(dp[i+1][j] , dp[i][j-1]) + a);
}
ll val = dp[i][j] + b;
val += c;
int x = i , y = j;
while(next[x][y] != -1){
int pos = next[x][y];
val += c;
val += (pos - (y + 1LL))*a;
dp[i][pos + len] = min(dp[i][pos+len],val);
x = pos;
y = pos + len;
}
}
}
cout << dp[0][n-1] << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}
Compilation message
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:67:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if(pos < v[hash_pos[i][j]].size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
copypaste3.cpp:38:6: warning: variable 'hash_val' set but not used [-Wunused-but-set-variable]
38 | int hash_val[n][n] , hash_pos[n][n];
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
493 ms |
153580 KB |
Output is correct |
4 |
Correct |
587 ms |
179420 KB |
Output is correct |
5 |
Correct |
728 ms |
209768 KB |
Output is correct |
6 |
Correct |
889 ms |
245572 KB |
Output is correct |
7 |
Correct |
1075 ms |
287780 KB |
Output is correct |
8 |
Correct |
1078 ms |
287568 KB |
Output is correct |
9 |
Correct |
1061 ms |
287532 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
856 KB |
Output is correct |
41 |
Correct |
3 ms |
2140 KB |
Output is correct |
42 |
Correct |
9 ms |
3932 KB |
Output is correct |
43 |
Correct |
10 ms |
3932 KB |
Output is correct |
44 |
Correct |
9 ms |
3932 KB |
Output is correct |
45 |
Correct |
10 ms |
4184 KB |
Output is correct |
46 |
Correct |
10 ms |
4028 KB |
Output is correct |
47 |
Correct |
6 ms |
2652 KB |
Output is correct |
48 |
Correct |
8 ms |
3160 KB |
Output is correct |
49 |
Correct |
8 ms |
3420 KB |
Output is correct |
50 |
Correct |
11 ms |
3672 KB |
Output is correct |
51 |
Correct |
7 ms |
3160 KB |
Output is correct |
52 |
Correct |
8 ms |
3164 KB |
Output is correct |
53 |
Correct |
10 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
2 ms |
856 KB |
Output is correct |
41 |
Correct |
3 ms |
2140 KB |
Output is correct |
42 |
Correct |
9 ms |
3932 KB |
Output is correct |
43 |
Correct |
10 ms |
3932 KB |
Output is correct |
44 |
Correct |
9 ms |
3932 KB |
Output is correct |
45 |
Correct |
10 ms |
4184 KB |
Output is correct |
46 |
Correct |
10 ms |
4028 KB |
Output is correct |
47 |
Correct |
6 ms |
2652 KB |
Output is correct |
48 |
Correct |
8 ms |
3160 KB |
Output is correct |
49 |
Correct |
8 ms |
3420 KB |
Output is correct |
50 |
Correct |
11 ms |
3672 KB |
Output is correct |
51 |
Correct |
7 ms |
3160 KB |
Output is correct |
52 |
Correct |
8 ms |
3164 KB |
Output is correct |
53 |
Correct |
10 ms |
3932 KB |
Output is correct |
54 |
Correct |
57 ms |
18080 KB |
Output is correct |
55 |
Correct |
113 ms |
46164 KB |
Output is correct |
56 |
Correct |
502 ms |
89628 KB |
Output is correct |
57 |
Correct |
500 ms |
89940 KB |
Output is correct |
58 |
Correct |
491 ms |
90168 KB |
Output is correct |
59 |
Correct |
493 ms |
90452 KB |
Output is correct |
60 |
Correct |
469 ms |
90192 KB |
Output is correct |
61 |
Correct |
314 ms |
63576 KB |
Output is correct |
62 |
Correct |
129 ms |
46600 KB |
Output is correct |
63 |
Correct |
420 ms |
78708 KB |
Output is correct |
64 |
Correct |
413 ms |
79444 KB |
Output is correct |
65 |
Correct |
321 ms |
67160 KB |
Output is correct |
66 |
Correct |
375 ms |
66908 KB |
Output is correct |
67 |
Correct |
516 ms |
90368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
493 ms |
153580 KB |
Output is correct |
14 |
Correct |
587 ms |
179420 KB |
Output is correct |
15 |
Correct |
728 ms |
209768 KB |
Output is correct |
16 |
Correct |
889 ms |
245572 KB |
Output is correct |
17 |
Correct |
1075 ms |
287780 KB |
Output is correct |
18 |
Correct |
1078 ms |
287568 KB |
Output is correct |
19 |
Correct |
1061 ms |
287532 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
464 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
856 KB |
Output is correct |
58 |
Correct |
3 ms |
2140 KB |
Output is correct |
59 |
Correct |
9 ms |
3932 KB |
Output is correct |
60 |
Correct |
10 ms |
3932 KB |
Output is correct |
61 |
Correct |
9 ms |
3932 KB |
Output is correct |
62 |
Correct |
10 ms |
4184 KB |
Output is correct |
63 |
Correct |
10 ms |
4028 KB |
Output is correct |
64 |
Correct |
6 ms |
2652 KB |
Output is correct |
65 |
Correct |
8 ms |
3160 KB |
Output is correct |
66 |
Correct |
8 ms |
3420 KB |
Output is correct |
67 |
Correct |
11 ms |
3672 KB |
Output is correct |
68 |
Correct |
7 ms |
3160 KB |
Output is correct |
69 |
Correct |
8 ms |
3164 KB |
Output is correct |
70 |
Correct |
10 ms |
3932 KB |
Output is correct |
71 |
Correct |
57 ms |
18080 KB |
Output is correct |
72 |
Correct |
113 ms |
46164 KB |
Output is correct |
73 |
Correct |
502 ms |
89628 KB |
Output is correct |
74 |
Correct |
500 ms |
89940 KB |
Output is correct |
75 |
Correct |
491 ms |
90168 KB |
Output is correct |
76 |
Correct |
493 ms |
90452 KB |
Output is correct |
77 |
Correct |
469 ms |
90192 KB |
Output is correct |
78 |
Correct |
314 ms |
63576 KB |
Output is correct |
79 |
Correct |
129 ms |
46600 KB |
Output is correct |
80 |
Correct |
420 ms |
78708 KB |
Output is correct |
81 |
Correct |
413 ms |
79444 KB |
Output is correct |
82 |
Correct |
321 ms |
67160 KB |
Output is correct |
83 |
Correct |
375 ms |
66908 KB |
Output is correct |
84 |
Correct |
516 ms |
90368 KB |
Output is correct |
85 |
Correct |
1589 ms |
223968 KB |
Output is correct |
86 |
Execution timed out |
3068 ms |
455608 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |