#include <bits/stdc++.h>
#define ar array
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector <int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) {
char c; cin >> c;
a[i] = c - '0';
}
for (int i = 1; i <= n; i++) {
char c; cin >> c;
b[i] = c - '0';
}
vector <vector <int>> dp(n + 1, vector <int> (2, 1e9));
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == b[i]) {
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][1] + 2;
continue;
}
vector <int> cnt(2, 0);
int now = 2, tog = 0;
for (int j = i; j >= 1; j--) {
if (b[j] != now)
cnt[now = b[j]]++;
if (a[j] == b[j])
tog = 1e9;
int v = min(cnt[0] + (cnt[1] > 0), cnt[1] + (cnt[0] > 0));
dp[i][0] = min(dp[i][0], min(dp[j - 1][0], dp[j - 1][1]) + v + (b[i] != b[j]));
dp[i][1] = min(dp[i][1], min(min(dp[j - 1][1], dp[j - 1][0] + 1) + tog, min(dp[j - 1][0] + 1, dp[j - 1][1]) + v));
}
}
// for (int i = 0; i < 2; i++, cout << '\n')
// for (int j = 1; j <= n; j++)
// cout << dp[j][i];
cout << min(dp[n][0], dp[n][1]);
}
/*
12
101010101010
010111111101
33
010101010101010101010101010101010
101000111000111000111000111000101
*/
# | 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... |