#include <iostream> using namespace std; const int N = 3e5 + 7; int out[N]; int get_split(int n, const string &s) { int ls = 0, ps = 0; for (auto it : s) { if (it == 'L') ls++; } int ret = -1; for (int i = 0; i <= n; i++) { if (ps == ls) ret = i; if (s[i] == 'P') ps++; else ls--; } return ret; } int main() { int n; string s; cin >> n; cin >> s; int split = get_split(n, s); int wyn = 0; char last = 'L'; for (int i = 0; i < split; i++) { if (s[i] == 'L') { if (last == 'P') wyn++; } else { wyn++; if (last == 'P') wyn++; } last = s[i]; out[i] = wyn; } last = 'P'; wyn = 0; for (int i = n - 1; i >= split; i--) { if (s[i] == 'P') { if (last == 'L') wyn++; } else { wyn++; if (last == 'L') wyn++; } last = s[i]; out[i] = wyn; } for (int i = 0; i < n; i++) cout << out[i] << " "; 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> using namespace std; const int N = 3e5 + 7; int out[N]; int get_split(int n, const string &s) { int ls = 0, ps = 0; for (auto it : s) { if (it == 'L') ls++; } int ret = -1; for (int i = 0; i <= n; i++) { if (ps == ls) ret = i; if (s[i] == 'P') ps++; else ls--; } return ret; } int main() { int n; string s; cin >> n; cin >> s; int split = get_split(n, s); int wyn = 0; char last = 'L'; for (int i = 0; i < split; i++) { if (s[i] == 'L') { if (last == 'P') wyn++; } else { wyn++; if (last == 'P') wyn++; } last = s[i]; out[i] = wyn; } last = 'P'; wyn = 0; for (int i = n - 1; i >= split; i--) { if (s[i] == 'P') { if (last == 'L') wyn++; } else { wyn++; if (last == 'L') wyn++; } last = s[i]; out[i] = wyn; } for (int i = 0; i < n; i++) cout << out[i] << " "; return 0; } |