#include<bits/stdc++.h>
using namespace std;
int dp[2][4][4][4], dp1[2][4], dp2[2][4][4];
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
string s, t;
cin>>n>>s>>t;
for(int i=0; i<=1; ++i) {
for(int a=0; a<=3; ++a) {
for(int b=0; b<=3; ++b) {
for(int c=0; c<=3; ++c) {
dp[i][a][b][c] = n;
}
}
}
}
dp[0][0][0][0]=0;
for(int a=1; a<=3; ++a) {
dp1[0][a]=n;
for(int b=1; b<=3; ++b) {
dp2[0][a][b]=n;
}
}
for(int i=1; i<=n; ++i) {
int v1 = n;
for(int a=0; a<=3; ++a) {
for(int b=0; b<=3; ++b) {
for(int c=0; c<=3; ++c) {
dp[i%2][a][b][c]=n;
v1 = min(v1, dp[(i+1)%2][a][b][c]);
}
}
}
if(s[i-1]==t[i-1]) {
dp[i%2][0][0][0] = v1;
}
for(int a=1; a<=3; ++a) {
if((a==1 && t[i-1]=='0') || (a==2 && t[i-1]=='1') || (a==3 && s[i-1]!=t[i-1])) {
dp[i%2][a][0][0] = min(v1+1, dp1[(i+1)%2][a]);
}
for(int b=1; b<=3; ++b) {
if(b==a) continue;
if((a==1 && t[i-1]=='0') || (a==2 && t[i-1]=='1') || (a==3 && ((b==1 && t[i-1]=='1') || (b==2 && t[i-1]=='0')))) {
dp[i%2][a][b][0] = min(v1+2, min(dp1[(i+1)%2][a], dp1[(i+1)%2][b])+1);
dp[i%2][a][b][0] = min(dp[i%2][a][b][0], dp2[(i+1)%2][a][b]);
}
else {
continue;
}
for(int c=1; c<=3; ++c) {
if(c==a || c==b) continue;
dp[i%2][a][b][c] = min(v1+3, min(dp1[(i+1)%2][a], min(dp1[(i+1)%2][b], dp1[(i+1)%2][c]))+2);
dp[i%2][a][b][c] = min(dp[i%2][a][b][c], min(dp2[(i+1)%2][a][b], min(dp2[(i+1)%2][a][c], dp2[(i+1)%2][b][c]))+1);
dp[i%2][a][b][c] = min(dp[i%2][a][b][c], dp[(i+1)%2][a][b][c]);
}
}
}
for(int a=1; a<=3; ++a) {
dp1[i%2][a]=n;
for(int j=0; j<=3; ++j) {
for(int k=0; k<=3; ++k) {
dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][a][j][k]);
dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][j][a][k]);
dp1[i%2][a] = min(dp1[i%2][a], dp[i%2][j][k][a]);
}
}
for(int b=1; b<=3; ++b) {
dp2[i%2][a][b]=n;
for(int j=0; j<=3; ++j) {
dp2[i%2][a][b] = min(dp2[i%2][a][b], dp[i%2][a][b][j]);
dp2[i%2][a][b] = min(dp2[i%2][a][b], dp[i%2][j][a][b]);
}
}
}
}
int ans = n;
for(int a=0; a<=3; ++a) {
for(int b=0; b<=3; ++b) {
for(int c=0; c<=3; ++c) {
ans = min(ans, dp[n%2][a][b][c]);
}
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
6 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
384 KB |
Output is correct |
50 |
Correct |
6 ms |
384 KB |
Output is correct |
51 |
Correct |
5 ms |
512 KB |
Output is correct |
52 |
Correct |
6 ms |
384 KB |
Output is correct |
53 |
Correct |
5 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
5 ms |
384 KB |
Output is correct |
58 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
290 ms |
4380 KB |
Output is correct |
8 |
Correct |
298 ms |
4380 KB |
Output is correct |
9 |
Correct |
302 ms |
4508 KB |
Output is correct |
10 |
Correct |
296 ms |
4380 KB |
Output is correct |
11 |
Correct |
303 ms |
4508 KB |
Output is correct |
12 |
Correct |
289 ms |
4380 KB |
Output is correct |
13 |
Correct |
291 ms |
4380 KB |
Output is correct |
14 |
Correct |
291 ms |
4380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
6 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
384 KB |
Output is correct |
50 |
Correct |
6 ms |
384 KB |
Output is correct |
51 |
Correct |
5 ms |
512 KB |
Output is correct |
52 |
Correct |
6 ms |
384 KB |
Output is correct |
53 |
Correct |
5 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
5 ms |
384 KB |
Output is correct |
58 |
Correct |
5 ms |
384 KB |
Output is correct |
59 |
Correct |
4 ms |
384 KB |
Output is correct |
60 |
Correct |
4 ms |
384 KB |
Output is correct |
61 |
Correct |
4 ms |
384 KB |
Output is correct |
62 |
Correct |
4 ms |
384 KB |
Output is correct |
63 |
Correct |
4 ms |
384 KB |
Output is correct |
64 |
Correct |
5 ms |
384 KB |
Output is correct |
65 |
Correct |
290 ms |
4380 KB |
Output is correct |
66 |
Correct |
298 ms |
4380 KB |
Output is correct |
67 |
Correct |
302 ms |
4508 KB |
Output is correct |
68 |
Correct |
296 ms |
4380 KB |
Output is correct |
69 |
Correct |
303 ms |
4508 KB |
Output is correct |
70 |
Correct |
289 ms |
4380 KB |
Output is correct |
71 |
Correct |
291 ms |
4380 KB |
Output is correct |
72 |
Correct |
291 ms |
4380 KB |
Output is correct |
73 |
Correct |
314 ms |
4556 KB |
Output is correct |
74 |
Correct |
297 ms |
4436 KB |
Output is correct |
75 |
Correct |
303 ms |
4436 KB |
Output is correct |
76 |
Correct |
304 ms |
4552 KB |
Output is correct |
77 |
Correct |
297 ms |
4436 KB |
Output is correct |
78 |
Correct |
304 ms |
4436 KB |
Output is correct |
79 |
Correct |
297 ms |
4436 KB |
Output is correct |
80 |
Correct |
312 ms |
4552 KB |
Output is correct |
81 |
Correct |
297 ms |
4508 KB |
Output is correct |
82 |
Correct |
297 ms |
4552 KB |
Output is correct |
83 |
Correct |
305 ms |
4548 KB |
Output is correct |
84 |
Correct |
301 ms |
4508 KB |
Output is correct |
85 |
Correct |
303 ms |
4548 KB |
Output is correct |
86 |
Correct |
306 ms |
4508 KB |
Output is correct |
87 |
Correct |
304 ms |
4548 KB |
Output is correct |
88 |
Correct |
306 ms |
4548 KB |
Output is correct |
89 |
Correct |
298 ms |
4436 KB |
Output is correct |
90 |
Correct |
306 ms |
4444 KB |
Output is correct |
91 |
Correct |
305 ms |
4436 KB |
Output is correct |
92 |
Correct |
311 ms |
4436 KB |
Output is correct |
93 |
Correct |
320 ms |
4448 KB |
Output is correct |
94 |
Correct |
312 ms |
4436 KB |
Output is correct |
95 |
Correct |
323 ms |
4436 KB |
Output is correct |
96 |
Correct |
318 ms |
4436 KB |
Output is correct |
97 |
Correct |
314 ms |
4552 KB |
Output is correct |
98 |
Correct |
321 ms |
4508 KB |
Output is correct |
99 |
Correct |
311 ms |
4436 KB |
Output is correct |
100 |
Correct |
310 ms |
4436 KB |
Output is correct |
101 |
Correct |
312 ms |
4436 KB |
Output is correct |
102 |
Correct |
319 ms |
4556 KB |
Output is correct |