#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Ant { int c; long left, right; Ant(int c) : c(c), left(0), right(0) { } long bounceCount() const { return left + right; } void setLeft(Ant const &a) { if (a.right) { right += a.right - (c == 'L'); left += a.right; c = 'P'; } if (c != 'L') { right++; c = 'L'; } } void setRight(Ant const &a) { if (a.left) { left += a.left - (c != 'L'); right += a.left; c = 'L'; } if (c == 'L') { left++; c = 'P'; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, i; string s; cin >> n >> s; int left_cnt = count(s.begin(), s.end(), 'L'); vector< Ant > v; v.reserve((n = s.length()) + 2); v.push_back('L'); for (i = 0; i < n; v.push_back(s[i++])) { } v.push_back('P'); for (i = 1; i <= left_cnt; ++i) v[i].setLeft(v[i - 1]); for (i = n; i > left_cnt; --i) v[i].setRight(v[i + 1]); char const *delim = ""; for (i = 1; i <= n; i++) { cout << delim << v[i].bounceCount(); delim = " "; } cout << '\n'; }
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 <vector> #include <algorithm> using namespace std; struct Ant { int c; long left, right; Ant(int c) : c(c), left(0), right(0) { } long bounceCount() const { return left + right; } void setLeft(Ant const &a) { if (a.right) { right += a.right - (c == 'L'); left += a.right; c = 'P'; } if (c != 'L') { right++; c = 'L'; } } void setRight(Ant const &a) { if (a.left) { left += a.left - (c != 'L'); right += a.left; c = 'L'; } if (c == 'L') { left++; c = 'P'; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, i; string s; cin >> n >> s; int left_cnt = count(s.begin(), s.end(), 'L'); vector< Ant > v; v.reserve((n = s.length()) + 2); v.push_back('L'); for (i = 0; i < n; v.push_back(s[i++])) { } v.push_back('P'); for (i = 1; i <= left_cnt; ++i) v[i].setLeft(v[i - 1]); for (i = n; i > left_cnt; --i) v[i].setRight(v[i + 1]); char const *delim = ""; for (i = 1; i <= n; i++) { cout << delim << v[i].bounceCount(); delim = " "; } cout << '\n'; } |