#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; vector<long long>v(n); vector<int>right; for (int i = 0; i < n; i++) if (s[i] == 'P') right.push_back(i); long long sm = 0; for (int i = n - 1; i >= 0; i--) { v[i] += sm; if (s[i] == 'P' && (!right.size() || right.back() != i)) { sm -= 2; v[i]--; } else if (s[i] == 'P') { right.pop_back(); continue; } if (right.size()) { right.pop_back(); sm += 2; v[i]++; } } for (auto& x : v) cout << x << ' '; return 0; }
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 | #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; vector<long long>v(n); vector<int>right; for (int i = 0; i < n; i++) if (s[i] == 'P') right.push_back(i); long long sm = 0; for (int i = n - 1; i >= 0; i--) { v[i] += sm; if (s[i] == 'P' && (!right.size() || right.back() != i)) { sm -= 2; v[i]--; } else if (s[i] == 'P') { right.pop_back(); continue; } if (right.size()) { right.pop_back(); sm += 2; v[i]++; } } for (auto& x : v) cout << x << ' '; return 0; } |