#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <int> v(n); for (int i = 0; i < n; i++) { char c; cin >> c; v[i] = (c == 'P'); } int cnt = 0; vector <int> l(n, 0), r(n, 0); for (int i = 0; i < n; i++) { l[i] = cnt; if (v[i]) cnt++; } cnt = 0; for (int i = n - 1; i >= 0; i--) { r[i] = cnt; if (!v[i]) cnt++; } vector <int> ans(n); for (int i = 0; i < cnt; i++) { if (v[i] && r[i]) ans[i] = 1 + min(l[i], r[i] - 1) * 2; else if (!v[i]) ans[i] = min(l[i], r[i]) * 2; } for (int i = cnt; i < n; i++) { if (!v[i] && l[i]) ans[i] = 1 + min(l[i] - 1, r[i]) * 2; else if (v[i]) ans[i] = min(l[i], r[i]) * 2; } for (auto x : ans) cout << x << ' '; cout << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <int> v(n); for (int i = 0; i < n; i++) { char c; cin >> c; v[i] = (c == 'P'); } int cnt = 0; vector <int> l(n, 0), r(n, 0); for (int i = 0; i < n; i++) { l[i] = cnt; if (v[i]) cnt++; } cnt = 0; for (int i = n - 1; i >= 0; i--) { r[i] = cnt; if (!v[i]) cnt++; } vector <int> ans(n); for (int i = 0; i < cnt; i++) { if (v[i] && r[i]) ans[i] = 1 + min(l[i], r[i] - 1) * 2; else if (!v[i]) ans[i] = min(l[i], r[i]) * 2; } for (int i = cnt; i < n; i++) { if (!v[i] && l[i]) ans[i] = 1 + min(l[i] - 1, r[i]) * 2; else if (v[i]) ans[i] = min(l[i], r[i]) * 2; } for (auto x : ans) cout << x << ' '; cout << '\n'; } |