#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; s = '#' + s + '#'; vector<int> sp (n+2, 0); vector<int> ss (n+2, 0); for (int i = 1; i < n+1; i++) { if (s[i-1] == 'P') { sp[i] = sp[i-1]+1; } else { sp[i] = sp[i-1]; } } for (int i = n; i > 0; i--) { if (s[i+1] == 'L') { ss[i] = ss[i+1]+1; } else { ss[i] = ss[i+1]; } } for (int i = 1; i < n+1; i++) { if (s[i] == 'L') { if (sp[i] <= ss[i]) { cout << sp[i]*2 << ' '; } else { cout << (ss[i]*2)+1 << ' '; } } else { if (ss[i] <= sp[i]) { cout << ss[i]*2 << ' '; } else { cout << (sp[i]*2)+1 << ' '; } } } }
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 48 49 | #include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; s = '#' + s + '#'; vector<int> sp (n+2, 0); vector<int> ss (n+2, 0); for (int i = 1; i < n+1; i++) { if (s[i-1] == 'P') { sp[i] = sp[i-1]+1; } else { sp[i] = sp[i-1]; } } for (int i = n; i > 0; i--) { if (s[i+1] == 'L') { ss[i] = ss[i+1]+1; } else { ss[i] = ss[i+1]; } } for (int i = 1; i < n+1; i++) { if (s[i] == 'L') { if (sp[i] <= ss[i]) { cout << sp[i]*2 << ' '; } else { cout << (ss[i]*2)+1 << ' '; } } else { if (ss[i] <= sp[i]) { cout << ss[i]*2 << ' '; } else { cout << (sp[i]*2)+1 << ' '; } } } } |