#include <cstdio> #include <vector> using namespace std; int main () { int n; scanf("%d\n", &n); vector<char> ants; ants.reserve(n + 8); char c; for (int i = 0; i < n; ++i) { scanf("%c", &c); ants.push_back(c); } vector<int> p_right(n + 1, 0); vector<int> p_left(n + 1, 0); for (int i = 1; i <= n; ++i) { p_right[i] = p_right[i - 1] + (ants[i - 1] == 'P' ? 1 : 0); } for (int i = n - 1; i >= 0; --i) { p_left[i] = p_left[i + 1] + (ants[i] == 'L' ? 1 : 0); } for (int i = 0; i < n; ++i) { printf("%d ", min(p_right[i + 1], p_left[i + 1]) + min(p_right[i], p_left[i])); } printf("\n"); 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 | #include <cstdio> #include <vector> using namespace std; int main () { int n; scanf("%d\n", &n); vector<char> ants; ants.reserve(n + 8); char c; for (int i = 0; i < n; ++i) { scanf("%c", &c); ants.push_back(c); } vector<int> p_right(n + 1, 0); vector<int> p_left(n + 1, 0); for (int i = 1; i <= n; ++i) { p_right[i] = p_right[i - 1] + (ants[i - 1] == 'P' ? 1 : 0); } for (int i = n - 1; i >= 0; --i) { p_left[i] = p_left[i + 1] + (ants[i] == 'L' ? 1 : 0); } for (int i = 0; i < n; ++i) { printf("%d ", min(p_right[i + 1], p_left[i + 1]) + min(p_right[i], p_left[i])); } printf("\n"); return 0; } |