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;
const int N = 1e6 + 5;
int n, dp[N], a[N], b[N], c[N], lstb0[N], lstb1[N], lstc1[N];
string s, t;
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s >> t; b[0] = -1;
for (int i = 0; i < n; i++) {
a[i + 1] = s[i] - '0';
b[i + 1] = t[i] - '0';
c[i + 1] = a[i + 1] ^ b[i + 1];
}
for (int i = 1; i <= n; i++) {
if (b[i] == 1) lstb1[i] = (b[i - 1] == 1 ? lstb1[i - 1] : i);
else lstb0[i] = (b[i - 1] == 0 ? lstb0[i - 1] : i);
if (c[i] == 1) lstc1[i] = (c[i - 1] == 1 ? lstc1[i - 1] : i);
}
memset(dp, 0x3f, sizeof(dp)); dp[0] = 0;
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1] + (a[i] == b[i] ? 0 : 1);
if (lstb0[i]) dp[i] = min(dp[i], dp[lstb0[i] - 1] + 1);
if (lstb1[i]) dp[i] = min(dp[i], dp[lstb1[i] - 1] + 1);
if (lstc1[i]) dp[i] = min(dp[i], dp[lstc1[i] - 1] + 1);
}
//for (int i = 1; i <= n; i++) cout << dp[i] << " ";
//cout << '\n';
cout << dp[n];
return 0;
}
# | 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... |