This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
int N; cin >> N; string A, B; cin >> A >> B;
int dp[N][3][2]; // idx, got 0 and got 1, got flip
if(A[0] == B[0]) dp[0][0][0] = 0;
else dp[0][0][0] = 1e9;
if(B[0] == 0) dp[0][1][0] = 1;
else dp[0][1][0] = 1e9;
if(B[0] == 1) dp[0][2][0] = 1;
else dp[0][2][0] = 1e9;
if(A[0] != B[0]) dp[0][0][1] = 1;
else dp[0][0][1] = 1e9;
dp[0][1][1] = 2; dp[0][2][1] = 2;
for(int i = 1; i < N; i++) {
int minprev = 1e9;
for(int j = 0; j < 3; j++) for(int k = 0; k < 2; k++) {
minprev = min(minprev, dp[i - 1][j][k]);
}
if(A[i] == B[i]) dp[i][0][0] = minprev;
else dp[i][0][0] = 1e9;
if(B[i] == '0') dp[i][1][0] = min(minprev + 1, min(dp[i - 1][1][0], dp[i - 1][1][1]));
else dp[i][1][0] = 1e9;
if(B[i] == '1') dp[i][2][0] = min(minprev + 1, min(dp[i - 1][2][0], dp[i - 1][2][1]));
else dp[i][2][0] = 1e9;
if(A[i] != B[i]) dp[i][0][1] = min(minprev + 1, min(dp[i - 1][0][1], min(dp[i - 1][1][1], dp[i - 1][2][1])));
else dp[i][0][1] = 1e9;
dp[i][1][1] = min(minprev + 2, min(dp[i - 1][1][0], dp[i - 1][1][1]) + 1);
dp[i][1][1] = min(dp[i][1][1], min(min(dp[i - 1][0][1], dp[i - 1][1][1]), dp[i - 1][2][1]) + 1);
if(B[i] == B[i - 1]) {
dp[i][1][1] = min(dp[i][1][1], dp[i - 1][1][1]);
}
dp[i][2][1] = min(minprev + 2, min(dp[i - 1][2][0], dp[i - 1][2][1]) + 1);
dp[i][2][1] = min(dp[i][2][1], min(min(dp[i - 1][0][1], dp[i - 1][1][1]), dp[i - 1][2][1]) + 1);
if(B[i] == B[i - 1]) {
dp[i][2][1] = min(dp[i][2][1], dp[i - 1][2][1]);
}
// for(int k = 0; k < 3; k++) for(int j = 0; j < 2; j++) printf("%d %d %d %d\n", i, k, j, dp[i][k][j]);
}
int ans = INT_MAX;
for(int i = 0; i < 3; i++) for(int j = 0; j < 2; j++) ans = min(ans, dp[N - 1][i][j]);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |