#include <iostream> #include <vector> using namespace std; void solve(string s, int n); int main() { int n; cin >> n; string s; cin >> s; solve(s, n); return 0; } void solve(string s, int n) { bool changedDir = true; vector<int> result(n, 0); int left = 0, right = n-1; while (changedDir or (left < n-1 or right >= 1)) { changedDir = false; if (s[left] == 'P' and s[left + 1] == 'L') { changedDir = true; result[left]++; result[left + 1]++; swap(s[left], s[left + 1]); } if (s[right] == 'L' and s[right - 1] == 'R') { changedDir = true; result[right]++; result[right - 1]++; swap(s[right], s[right - 1]); } if (left < n-1) left++; if (right > 0) right--; if (changedDir == true and (left >= n - 1 or right <= 1)) { swap(left, right); } } for (int i = 0; i < n; i++) { cout << result[i] << " "; } }
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 50 51 52 53 | #include <iostream> #include <vector> using namespace std; void solve(string s, int n); int main() { int n; cin >> n; string s; cin >> s; solve(s, n); return 0; } void solve(string s, int n) { bool changedDir = true; vector<int> result(n, 0); int left = 0, right = n-1; while (changedDir or (left < n-1 or right >= 1)) { changedDir = false; if (s[left] == 'P' and s[left + 1] == 'L') { changedDir = true; result[left]++; result[left + 1]++; swap(s[left], s[left + 1]); } if (s[right] == 'L' and s[right - 1] == 'R') { changedDir = true; result[right]++; result[right - 1]++; swap(s[right], s[right - 1]); } if (left < n-1) left++; if (right > 0) right--; if (changedDir == true and (left >= n - 1 or right <= 1)) { swap(left, right); } } for (int i = 0; i < n; i++) { cout << result[i] << " "; } } |