#include <stdio.h>
#include <string.h>
#define N 2500
#define A 26
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
static char cc[N + 1];
static long long dp[N][N];
static int lcp[N + 1][N + 1], ii[N + 1], jj[N + 1], kk[A + 1];
int n, i, j, k, l, a;
long long u, v, w;
scanf("%d%s%lld%lld%lld", &n, cc, &u, &v, &w);
for (i = n - 1; i >= 0; i--)
for (j = n - 1; j >= 0; j--)
lcp[i][j] = cc[i] == cc[j] ? lcp[i + 1][j + 1] + 1 : 0;
for (i = 0; i < n; i++)
memset(dp[i] + i, 0x3f, (n - i + 1) * sizeof *dp[i]), dp[i][i] = u;
for (i = 0; i <= n; i++)
ii[i] = i;
for (l = 1; l < n; l++) {
memset(kk, 0, (A + 1) * sizeof *kk);
for (i = 0; i <= n - l + 1; i++) {
j = --ii[i];
if (j < 0)
continue;
kk[cc[j] - 'a' + 1]++;
}
for (a = 1; a <= A; a++)
kk[a] += kk[a - 1];
for (i = 0; i <= n - l + 1; i++)
if ((j = ii[i]) >= 0)
jj[kk[cc[j] - 'a']++] = j;
memcpy(ii, jj, (n - l + 1) * sizeof *jj);
memset(jj, -1, n * sizeof *jj);
for (i = 0, j = 0; i < n - l + 1; i++) {
while (j < n - l + 1 && (j < i || lcp[ii[i]][ii[j]] >= l && ii[j] - ii[i] < l))
j++;
if (j < n - l + 1 && lcp[ii[i]][ii[j]] >= l)
jj[ii[i]] = ii[j];
}
for (i = 0; (j = i + l - 1) < n; i++) {
long long x = dp[i][j];
if (x == INF)
continue;
if (i > 0)
dp[i - 1][j] = min(dp[i - 1][j], x + u);
if (j + 1 < n)
dp[i][j + 1] = min(dp[i][j + 1], x + u);
for (j = jj[i], k = 2; j != -1; j = jj[j], k++)
dp[i][j + l - 1] = min(dp[i][j + l - 1], x + u * (j - i + l - k * l) + v + w * k);
}
}
printf("%lld\n", dp[0][n - 1]);
return 0;
}
Compilation message
copypaste3.c: In function 'main':
copypaste3.c:41:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
41 | while (j < n - l + 1 && (j < i || lcp[ii[i]][ii[j]] >= l && ii[j] - ii[i] < l))
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
copypaste3.c:17:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d%s%lld%lld%lld", &n, cc, &u, &v, &w);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
153 ms |
38652 KB |
Output is correct |
4 |
Correct |
172 ms |
42852 KB |
Output is correct |
5 |
Correct |
207 ms |
47632 KB |
Output is correct |
6 |
Correct |
248 ms |
52692 KB |
Output is correct |
7 |
Correct |
310 ms |
58196 KB |
Output is correct |
8 |
Correct |
287 ms |
58276 KB |
Output is correct |
9 |
Correct |
282 ms |
58276 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
288 KB |
Output is correct |
21 |
Correct |
0 ms |
544 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
480 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
296 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
288 KB |
Output is correct |
21 |
Correct |
0 ms |
544 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
480 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
296 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
552 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
2212 KB |
Output is correct |
42 |
Correct |
2 ms |
2132 KB |
Output is correct |
43 |
Correct |
2 ms |
2212 KB |
Output is correct |
44 |
Correct |
2 ms |
2132 KB |
Output is correct |
45 |
Correct |
1 ms |
2260 KB |
Output is correct |
46 |
Correct |
2 ms |
2208 KB |
Output is correct |
47 |
Correct |
2 ms |
2212 KB |
Output is correct |
48 |
Correct |
2 ms |
2132 KB |
Output is correct |
49 |
Correct |
2 ms |
2212 KB |
Output is correct |
50 |
Correct |
2 ms |
2132 KB |
Output is correct |
51 |
Correct |
2 ms |
2132 KB |
Output is correct |
52 |
Correct |
1 ms |
2132 KB |
Output is correct |
53 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
288 KB |
Output is correct |
21 |
Correct |
0 ms |
544 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
480 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
296 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
552 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
2212 KB |
Output is correct |
42 |
Correct |
2 ms |
2132 KB |
Output is correct |
43 |
Correct |
2 ms |
2212 KB |
Output is correct |
44 |
Correct |
2 ms |
2132 KB |
Output is correct |
45 |
Correct |
1 ms |
2260 KB |
Output is correct |
46 |
Correct |
2 ms |
2208 KB |
Output is correct |
47 |
Correct |
2 ms |
2212 KB |
Output is correct |
48 |
Correct |
2 ms |
2132 KB |
Output is correct |
49 |
Correct |
2 ms |
2212 KB |
Output is correct |
50 |
Correct |
2 ms |
2132 KB |
Output is correct |
51 |
Correct |
2 ms |
2132 KB |
Output is correct |
52 |
Correct |
1 ms |
2132 KB |
Output is correct |
53 |
Correct |
1 ms |
2132 KB |
Output is correct |
54 |
Correct |
6 ms |
5460 KB |
Output is correct |
55 |
Correct |
30 ms |
16152 KB |
Output is correct |
56 |
Correct |
25 ms |
16084 KB |
Output is correct |
57 |
Correct |
25 ms |
16084 KB |
Output is correct |
58 |
Correct |
25 ms |
16196 KB |
Output is correct |
59 |
Correct |
24 ms |
16160 KB |
Output is correct |
60 |
Correct |
23 ms |
16192 KB |
Output is correct |
61 |
Correct |
24 ms |
16084 KB |
Output is correct |
62 |
Correct |
31 ms |
16084 KB |
Output is correct |
63 |
Correct |
24 ms |
16148 KB |
Output is correct |
64 |
Correct |
26 ms |
16188 KB |
Output is correct |
65 |
Correct |
36 ms |
16188 KB |
Output is correct |
66 |
Correct |
34 ms |
16100 KB |
Output is correct |
67 |
Correct |
22 ms |
16196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
153 ms |
38652 KB |
Output is correct |
14 |
Correct |
172 ms |
42852 KB |
Output is correct |
15 |
Correct |
207 ms |
47632 KB |
Output is correct |
16 |
Correct |
248 ms |
52692 KB |
Output is correct |
17 |
Correct |
310 ms |
58196 KB |
Output is correct |
18 |
Correct |
287 ms |
58276 KB |
Output is correct |
19 |
Correct |
282 ms |
58276 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
288 KB |
Output is correct |
38 |
Correct |
0 ms |
544 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
480 KB |
Output is correct |
44 |
Correct |
0 ms |
468 KB |
Output is correct |
45 |
Correct |
0 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
0 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
296 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
0 ms |
552 KB |
Output is correct |
57 |
Correct |
1 ms |
980 KB |
Output is correct |
58 |
Correct |
2 ms |
2212 KB |
Output is correct |
59 |
Correct |
2 ms |
2132 KB |
Output is correct |
60 |
Correct |
2 ms |
2212 KB |
Output is correct |
61 |
Correct |
2 ms |
2132 KB |
Output is correct |
62 |
Correct |
1 ms |
2260 KB |
Output is correct |
63 |
Correct |
2 ms |
2208 KB |
Output is correct |
64 |
Correct |
2 ms |
2212 KB |
Output is correct |
65 |
Correct |
2 ms |
2132 KB |
Output is correct |
66 |
Correct |
2 ms |
2212 KB |
Output is correct |
67 |
Correct |
2 ms |
2132 KB |
Output is correct |
68 |
Correct |
2 ms |
2132 KB |
Output is correct |
69 |
Correct |
1 ms |
2132 KB |
Output is correct |
70 |
Correct |
1 ms |
2132 KB |
Output is correct |
71 |
Correct |
6 ms |
5460 KB |
Output is correct |
72 |
Correct |
30 ms |
16152 KB |
Output is correct |
73 |
Correct |
25 ms |
16084 KB |
Output is correct |
74 |
Correct |
25 ms |
16084 KB |
Output is correct |
75 |
Correct |
25 ms |
16196 KB |
Output is correct |
76 |
Correct |
24 ms |
16160 KB |
Output is correct |
77 |
Correct |
23 ms |
16192 KB |
Output is correct |
78 |
Correct |
24 ms |
16084 KB |
Output is correct |
79 |
Correct |
31 ms |
16084 KB |
Output is correct |
80 |
Correct |
24 ms |
16148 KB |
Output is correct |
81 |
Correct |
26 ms |
16188 KB |
Output is correct |
82 |
Correct |
36 ms |
16188 KB |
Output is correct |
83 |
Correct |
34 ms |
16100 KB |
Output is correct |
84 |
Correct |
22 ms |
16196 KB |
Output is correct |
85 |
Correct |
61 ms |
31828 KB |
Output is correct |
86 |
Correct |
154 ms |
58280 KB |
Output is correct |
87 |
Correct |
161 ms |
58280 KB |
Output is correct |
88 |
Correct |
139 ms |
58280 KB |
Output is correct |
89 |
Correct |
137 ms |
58280 KB |
Output is correct |
90 |
Correct |
133 ms |
58276 KB |
Output is correct |
91 |
Correct |
197 ms |
58280 KB |
Output is correct |
92 |
Correct |
271 ms |
58288 KB |
Output is correct |
93 |
Correct |
161 ms |
58280 KB |
Output is correct |
94 |
Correct |
154 ms |
58280 KB |
Output is correct |
95 |
Correct |
227 ms |
58280 KB |
Output is correct |
96 |
Correct |
225 ms |
58280 KB |
Output is correct |
97 |
Correct |
131 ms |
58192 KB |
Output is correct |