#include <iostream> #include <vector> using namespace std; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); int length; cin >> length; string input = "#", temp; cin >> temp; input += temp; vector<int> prefix_sums_L(length + 1, 0); vector<int> prefix_sums_P(length + 1, 0); for(int i = 1; i <= length; i ++){ prefix_sums_L[i] = prefix_sums_L[i - 1]; prefix_sums_P[i] = prefix_sums_P[i - 1]; if(input[i] == 'P') prefix_sums_P[i] ++; else prefix_sums_L[i] ++; } int first_result, second_result; for(int i = 1; i <= length; i ++){ if(input[i] == 'L'){ if(prefix_sums_P[i] == 0){ cout << "0 "; continue; } first_result = prefix_sums_P[i] * 2; if(prefix_sums_L[length] - prefix_sums_L[i] == 0) first_result -= 1; second_result = (prefix_sums_L[length] - prefix_sums_L[i]) * 2 + 1; //cout << i << " X " << first_result << " " << second_result << '\n'; } else { if(prefix_sums_L[length] - prefix_sums_L[i] == 0){ cout << "0 "; continue; } first_result = prefix_sums_P[i - 1] * 2 + 1; second_result = (prefix_sums_L[length] - prefix_sums_L[i]) * 2; if(prefix_sums_P[i - 1] == 0) second_result -= 1; //cout << i << " Y " << first_result << " " << second_result << '\n'; } cout << min(first_result, second_result) << " "; } 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 | #include <iostream> #include <vector> using namespace std; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); int length; cin >> length; string input = "#", temp; cin >> temp; input += temp; vector<int> prefix_sums_L(length + 1, 0); vector<int> prefix_sums_P(length + 1, 0); for(int i = 1; i <= length; i ++){ prefix_sums_L[i] = prefix_sums_L[i - 1]; prefix_sums_P[i] = prefix_sums_P[i - 1]; if(input[i] == 'P') prefix_sums_P[i] ++; else prefix_sums_L[i] ++; } int first_result, second_result; for(int i = 1; i <= length; i ++){ if(input[i] == 'L'){ if(prefix_sums_P[i] == 0){ cout << "0 "; continue; } first_result = prefix_sums_P[i] * 2; if(prefix_sums_L[length] - prefix_sums_L[i] == 0) first_result -= 1; second_result = (prefix_sums_L[length] - prefix_sums_L[i]) * 2 + 1; //cout << i << " X " << first_result << " " << second_result << '\n'; } else { if(prefix_sums_L[length] - prefix_sums_L[i] == 0){ cout << "0 "; continue; } first_result = prefix_sums_P[i - 1] * 2 + 1; second_result = (prefix_sums_L[length] - prefix_sums_L[i]) * 2; if(prefix_sums_P[i - 1] == 0) second_result -= 1; //cout << i << " Y " << first_result << " " << second_result << '\n'; } cout << min(first_result, second_result) << " "; } return 0; } |