#include <iostream> #include <unordered_map> using namespace std; int main() { int n; cin >> n; string directions; cin >> directions; int counter = 0; unordered_map<int, int> beginRange, endRange; for(int i = 0; i < directions.size(); i++) { char direction = directions[i]; if (direction == 'P') { counter++; } else if (counter > 0) { beginRange[i-counter+1]++; endRange[i]++; } } counter = 0; for(int i = directions.size()-1; i >= 0; i--) { char direction = directions[i]; if (direction == 'L') { counter++; } else if (counter > 0) { beginRange[i]++; endRange[i+counter-1]++; } } counter = 0; for(int i = 0; i < directions.size(); i++) { counter += beginRange[i]; cout << counter; cout << " "; counter -= endRange[i]; } cout << endl; }
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 | #include <iostream> #include <unordered_map> using namespace std; int main() { int n; cin >> n; string directions; cin >> directions; int counter = 0; unordered_map<int, int> beginRange, endRange; for(int i = 0; i < directions.size(); i++) { char direction = directions[i]; if (direction == 'P') { counter++; } else if (counter > 0) { beginRange[i-counter+1]++; endRange[i]++; } } counter = 0; for(int i = directions.size()-1; i >= 0; i--) { char direction = directions[i]; if (direction == 'L') { counter++; } else if (counter > 0) { beginRange[i]++; endRange[i+counter-1]++; } } counter = 0; for(int i = 0; i < directions.size(); i++) { counter += beginRange[i]; cout << counter; cout << " "; counter -= endRange[i]; } cout << endl; } |