#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
#define all(x) (x).begin(), (x).end()
const int A = 11; // number of characters + 1
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n = 0, nin;
cin >> nin;
string s;
cin >> s;
// preperation
vi text, underlined;
int deleteWork = 0;
bool underline;
for (int i = 0; i < nin; ++i)
{
if (s[i] == 'e')
{
++deleteWork;
if (n > 0)
{ // not leading 'e's
++deleteWork;
underline = true;
}
}
else
{
text.push_back(s[i] - 'a');
++n;
underlined.push_back(underline);
underline = false;
}
}
// DP
vvi P(n + 1, vi(A, 1e9));
vvvi Q(n + 1, vvi(A, vi(A, 1e9)));
P[0][text[0]] = 0;
for (int i = 1; i <= n; ++i)
{
for (int c = 0; c < A; ++c)
{
// fill P
int h = 1e9;
if (c != text[i - 1] && !underlined[i - 1])
h = min(h, P[i - 1][c]);
h = min(h, P[i - 1][text[i - 1]] + 2);
if (c != text[i - 1])
h = min(h, Q[i - 1][text[i - 1]][c]);
h = min(h, Q[i - 1][text[i - 1]][text[i - 1]] + 2);
P[i][c] = h;
// fill Q
for (int d = 0; d < A; ++d)
{
h = 1e9;
if (c != text[i - 1])
h = min(h, P[i - 1][c] + 3);
h = min(h, P[i - 1][text[i - 1]] + 5);
if (c != text[i - 1] && d != text[i - 1])
h = min(h, Q[i - 1][c][d] + 1);
if (c != text[i - 1])
h = min(h, Q[i - 1][c][text[i - 1]] + 3);
if (d != text[i - 1])
h = min(h, Q[i - 1][text[i - 1]][d] + 3);
h = min(h, Q[i - 1][text[i - 1]][text[i - 1]] + 5);
Q[i][c][d] = h;
}
}
}
cout << P[n][A - 1] + deleteWork - 2 << "\n";
return 0;
}
Compilation message
vim.cpp: In function 'int main()':
vim.cpp:40:34: warning: 'underline' may be used uninitialized in this function [-Wmaybe-uninitialized]
40 | underlined.push_back(underline);
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
616 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
576 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
576 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2260 KB |
Output is correct |
2 |
Correct |
4 ms |
3156 KB |
Output is correct |
3 |
Correct |
3 ms |
1748 KB |
Output is correct |
4 |
Correct |
3 ms |
2260 KB |
Output is correct |
5 |
Correct |
5 ms |
3284 KB |
Output is correct |
6 |
Correct |
6 ms |
4180 KB |
Output is correct |
7 |
Correct |
4 ms |
2772 KB |
Output is correct |
8 |
Correct |
4 ms |
2756 KB |
Output is correct |
9 |
Correct |
5 ms |
3156 KB |
Output is correct |
10 |
Correct |
5 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
48272 KB |
Output is correct |
2 |
Correct |
62 ms |
46104 KB |
Output is correct |
3 |
Correct |
68 ms |
48792 KB |
Output is correct |
4 |
Correct |
77 ms |
49552 KB |
Output is correct |
5 |
Correct |
83 ms |
55256 KB |
Output is correct |
6 |
Correct |
38 ms |
25164 KB |
Output is correct |
7 |
Correct |
59 ms |
41344 KB |
Output is correct |
8 |
Correct |
68 ms |
48152 KB |
Output is correct |
9 |
Correct |
50 ms |
35644 KB |
Output is correct |
10 |
Correct |
52 ms |
33912 KB |
Output is correct |
11 |
Correct |
76 ms |
49568 KB |
Output is correct |
12 |
Correct |
83 ms |
55208 KB |
Output is correct |
13 |
Correct |
86 ms |
54396 KB |
Output is correct |
14 |
Correct |
81 ms |
53392 KB |
Output is correct |
15 |
Correct |
70 ms |
44904 KB |
Output is correct |
16 |
Correct |
71 ms |
48992 KB |
Output is correct |
17 |
Correct |
69 ms |
48256 KB |
Output is correct |
18 |
Correct |
68 ms |
48808 KB |
Output is correct |
19 |
Correct |
64 ms |
46276 KB |
Output is correct |
20 |
Correct |
68 ms |
38952 KB |
Output is correct |