#include<iostream> #include<string> #include<stack> char arr[300005]; int res[300005]; int arr2[300005]; int arr3[300005]; int res2[300006]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, k = 0, p = 0; std::stack<int>s,s1; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } for (int i = n - 1; i >= 0; i--) { if (arr[i] == 'P') { arr2[i] = k; k++; s.push(i); } } for (int i = 0; i < n; i++) { if (arr[i] == 'L') { arr3[i] = p; p++; s1.push(i); } } while (!s1.empty()) { int t = s1.top(); res[arr3[t]+1] += 1; res[t + 1] -= 1; s1.pop(); } while (!s.empty()) { int t = s.top(); res[t] += 1; res[n - arr2[t] - 1] -= 1; s.pop(); } int o = 0; for (int i = 0; i < n; i++) { o += res[i]; res2[i] = o; } for (int i = 0; i < n; i++) { std::cout << res2[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 54 55 56 57 58 59 | #include<iostream> #include<string> #include<stack> char arr[300005]; int res[300005]; int arr2[300005]; int arr3[300005]; int res2[300006]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, k = 0, p = 0; std::stack<int>s,s1; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } for (int i = n - 1; i >= 0; i--) { if (arr[i] == 'P') { arr2[i] = k; k++; s.push(i); } } for (int i = 0; i < n; i++) { if (arr[i] == 'L') { arr3[i] = p; p++; s1.push(i); } } while (!s1.empty()) { int t = s1.top(); res[arr3[t]+1] += 1; res[t + 1] -= 1; s1.pop(); } while (!s.empty()) { int t = s.top(); res[t] += 1; res[n - arr2[t] - 1] -= 1; s.pop(); } int o = 0; for (int i = 0; i < n; i++) { o += res[i]; res2[i] = o; } for (int i = 0; i < n; i++) { std::cout << res2[i] << " "; } } |