#include <bits/stdc++.h> using namespace std; array<int, 300000+ 5> changes; string LP; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; cin >> LP; int begin {}, end{n-1}, i {}; while(begin <= end && LP[begin] == 'L') ++begin; while(end >= begin && LP[end] == 'P') --end; while(begin <= end) { for(i = begin; i < end; ++i) { if(LP[i] != LP[i + 1]) { changes[i]++; changes[i + 1]++; swap(LP[i], LP[i+1]); } } while(begin <= end && LP[begin] == 'L') ++begin; while(end >= begin && LP[end] == 'P') --end; } for(int i {}; i < n; ++i) cout << changes[i] << ' '; 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 | #include <bits/stdc++.h> using namespace std; array<int, 300000+ 5> changes; string LP; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; cin >> LP; int begin {}, end{n-1}, i {}; while(begin <= end && LP[begin] == 'L') ++begin; while(end >= begin && LP[end] == 'P') --end; while(begin <= end) { for(i = begin; i < end; ++i) { if(LP[i] != LP[i + 1]) { changes[i]++; changes[i + 1]++; swap(LP[i], LP[i+1]); } } while(begin <= end && LP[begin] == 'L') ++begin; while(end >= begin && LP[end] == 'P') --end; } for(int i {}; i < n; ++i) cout << changes[i] << ' '; cout << '\n'; } |