1
+ /* *
2
+ * @file 2687.cpp
3
+ * @author Macesuted ([email protected] )
4
+ * @date 2022-08-31
5
+ *
6
+ * @copyright Copyright (c) 2022
7
+ *
8
+ */
9
+
10
+ #include < bits/stdc++.h>
11
+ using namespace std ;
12
+
13
+ #ifndef LOCAL
14
+ #define endl ' \n '
15
+ #endif
16
+
17
+ bool mem1;
18
+
19
+ #define maxn 70005
20
+
21
+ int f[maxn][11 ], g[maxn][11 ][11 ];
22
+ bool mu[maxn];
23
+
24
+ void solve (void ) {
25
+ int n, cnt = 0 ;
26
+ string s;
27
+ cin >> n >> s, s = " " + s;
28
+ bool non = true ;
29
+ for (int i = 1 ; i <= n; i++)
30
+ if (s[i] == ' e' )
31
+ cnt += (non = true );
32
+ else
33
+ mu[i] = non, non = false ;
34
+ memset (f, 0x3f , sizeof (f)), memset (g, 0x3f , sizeof (g));
35
+ f[0 ][s[1 ] - ' a' ] = 0 ;
36
+ for (int i = 1 , l = 0 ; i <= n; l = i, i++) {
37
+ while (s[i] == ' e' ) i++;
38
+ for (int j = 0 ; j <= 10 ; j++) {
39
+ f[i][j] = min (f[l][s[i] - ' a' ] + 2 , g[l][s[i] - ' a' ][s[i] - ' a' ] + 2 );
40
+ if (j != s[i] - ' a' && !mu[i]) f[i][j] = min (f[i][j], f[l][j]);
41
+ if (j != s[i] - ' a' ) f[i][j] = min (f[i][j], g[l][s[i] - ' a' ][j]);
42
+ for (int k = 0 ; k <= 10 ; k++) {
43
+ g[i][j][k] = min (f[l][s[i] - ' a' ] + 5 , g[l][s[i] - ' a' ][s[i] - ' a' ] + 5 );
44
+ if (j != s[i] - ' a' ) g[i][j][k] = min ({g[i][j][k], f[l][j] + 3 , g[l][j][s[i] - ' a' ] + 3 });
45
+ if (k != s[i] - ' a' ) g[i][j][k] = min (g[i][j][k], g[l][s[i] - ' a' ][k] + 3 );
46
+ if (j != s[i] - ' a' && k != s[i] - ' a' ) g[i][j][k] = min (g[i][j][k], g[l][j][k] + 1 );
47
+ }
48
+ }
49
+ }
50
+ cout << f[n][10 ] + 2 * cnt - 2 << endl;
51
+ return ;
52
+ }
53
+
54
+ bool mem2;
55
+
56
+ int main () {
57
+ ios::sync_with_stdio (false ), cin.tie (nullptr );
58
+ #ifdef LOCAL
59
+ cerr << " Memory Cost: " << abs (&mem1 - &mem2) / 1024 . / 1024 . << " MB" << endl;
60
+ #endif
61
+
62
+ int _ = 1 ;
63
+ while (_--) solve ();
64
+
65
+ #ifdef LOCAL
66
+ cerr << " Time Cost: " << clock () * 1000 . / CLOCKS_PER_SEC << " MS" << endl;
67
+ #endif
68
+ return 0 ;
69
+ }
0 commit comments