#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 1e9;
int dp[2][3][2];
// first
// memory optimization
// second
// 0 - all 0,
// 1 - all 1
// 2 - no changes,
// thrid
// 0 not have xor
// 1 have xor
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, cr = 1, pr = 0;
string a, b;
cin >> n >> a >> b;
dp[pr][0][1] = dp[pr][1][1] = dp[pr][2][1] = dp[pr][0][0] = dp[pr][1][1] = dp[pr][2][0] = inf;
for(int i = 0; i < n; ++ i) {
dp[cr][0][0] = dp[cr][0][1] = dp[cr][2][0] =
dp[cr][1][0] = dp[cr][1][1] = dp[cr][2][1] = inf;
if(a[i] == b[i]) {
dp[cr][2][0] = min({dp[pr][2][0], dp[pr][2][1], dp[pr][1][0], dp[pr][1][1], dp[pr][0][0], dp[pr][0][1]});
} else {
dp[cr][2][1] = min({dp[pr][2][0] + 1, dp[pr][2][1], dp[pr][1][0] + 1, dp[pr][1][1], dp[pr][0][0] + 1, dp[pr][0][1]});
}
if(b[i] == '0') {
dp[cr][0][0] = min({dp[pr][0][0], dp[pr][2][0] + 1, dp[pr][2][1] + 1,
dp[pr][0][1], dp[pr][1][0] + 1, dp[pr][1][1] + 1});
dp[cr][1][1] = min({dp[pr][1][1], dp[pr][1][0] + 1, dp[pr][2][1] + 1,
dp[pr][0][1] + 1, dp[pr][2][0] + 2, dp[pr][0][0] + 2});
} else {
dp[cr][1][0] = min({dp[pr][1][0], dp[pr][2][0] + 1, dp[pr][2][1] + 1,
dp[pr][1][1], dp[pr][0][0] + 1, dp[pr][0][1] + 1});
dp[cr][0][1] = min({dp[pr][0][1], dp[pr][0][0] + 1, dp[pr][2][1] + 1,
dp[pr][1][1] + 1, dp[pr][2][0] + 2, dp[pr][1][0] + 2});
}
swap(cr, pr);
}
cout << 1 + min({dp[pr][0][0], dp[pr][0][1], dp[pr][1][0], dp[pr][1][1], dp[pr][2][0], dp[pr][2][1]});
}
Compilation message
lamp.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main() {
| ^~~~
lamp.cpp: In function 'int main()':
lamp.cpp:37:33: warning: operation on 'dp[pr][1][1]' may be undefined [-Wsequence-point]
37 | dp[pr][0][1] = dp[pr][1][1] = dp[pr][2][1] = dp[pr][0][0] = dp[pr][1][1] = dp[pr][2][0] = inf;
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |