#include <bits/stdc++.h>
using namespace std;
int n;
string a, b;
int dp[2100][2][3] = {0};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> a >> b;
for (char &c : a) c -= '0';
for (char &c : b) c -= '0';
a = "$"+a, b = "$"+b;
memset(dp, 63, sizeof(dp));
dp[0][0][2] = 0;
for (int i = 1; i <= n; i++) {
for (int lst = 0; lst <= 2; lst++) {
if (a[i] == b[i]) {
int z = min(dp[i-1][1][lst], dp[i-1][0][lst]);
dp[i][0][2] = min(dp[i][0][2], z);
}
if (b[i] == 0) {
dp[i][0][0] = min(dp[i][0][0], dp[i-1][0][lst] + !(lst==0));
dp[i][1][0] = min(dp[i][1][0], dp[i-1][1][lst] + !(lst==0));
dp[i][1][0] = min(dp[i][1][0], dp[i-1][0][1]+1 );
}
if (b[i] == 1) {
dp[i][0][1] = min(dp[i][0][1], dp[i-1][0][lst] + !(lst==1));
dp[i][1][1] = min(dp[i][1][1], dp[i-1][1][lst] + !(lst==1));
dp[i][1][1] = min(dp[i][1][1], dp[i-1][0][0]+1 );
}
if (a[i] != b[i]) {
for (int j = i; j <= n and b[j] != a[j]; j++) dp[j][1][2] = min(dp[j][1][2], min(dp[i-1][1][lst], dp[i-1][0][lst]+1));
}
}
}
int ans = INT_MAX;
for (int j = 0; j < 3; j++) {
// cout << dp[n][0][j] << ' ' << dp[n][1][j] << '\n';
ans = min(ans, min(dp[n][0][j], dp[n][1][j]));
}
cout << ans << '\n';
}
/*
5
01100
11001
01100
11110
11001
5
00110
01111
10011
*/
# | 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... |