-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (44 loc) · 1.05 KB
/
Copy pathmain.cpp
File metadata and controls
47 lines (44 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
auto calc = [](int n, string s) {
vector ret(2 * n, 0);
for (int i = 0, x = n; i < n; i++) {
int nx = x + (s[i] == '1' ? 1 : -1);
ret[min(x, nx)]++;
x = nx;
}
return ret;
};
auto sol = [](int n, string a, string b) {
if (calc(n, a) != calc(n, b)) return vector(1, pair(-1, -1));
vector ret(0, pair(0, 0));
while (a != b) {
int l = 0;
while (a[l] == b[l]) l++;
int r = l;
int x = a[r] == '1' ? 1 : -1;
while (r < n && (a[r] != a[l] || x != 0)) {
r++;
x += a[r] == '1' ? 1 : -1;
}
assert(r < n);
ret.push_back(pair(l, r));
reverse(a.begin() + l, a.begin() + r + 1);
for (int i = l; i <= r; i++) a[i] ^= 1;
}
return ret;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
string a, b; cin >> a >> b;
auto res = sol(n, a, b);
if (res != vector(1, pair(-1, -1))) {
cout << "YES" << '\n';
cout << res.size() << '\n';
for (auto [l, r] : res) cout << l + 1 << ' ' << r + 1 << '\n';
}
else {
cout << "NO" << '\n';
}
}