#include <iostream> #include <string> using namespace std; const int MAXN = 300009; string ants; int meetsL[MAXN] = {0}; int meetsP[MAXN] = {0}; int meets[MAXN] = {0}; int main() { int n; cin >> n; cin >> ants; size_t pos_first_P = ants.find('P'); size_t pos_last_L = ants.find_last_of('L'); int margL = (pos_first_P != string::npos) ? pos_first_P : n; int margP = (pos_last_L != string::npos) ? n - pos_last_L - 1 : n; if (margL + margP < n) { meetsL[margL] = 1; for (int i = margL + 1; i < n - margP; i++) { if (ants[i] == ants[i - 1]) { if (ants[i] == 'L') { meetsL[i] = meetsL[i - 1]; } else { meetsL[i] = meetsL[i - 1] + 2; } } else { meetsL[i] = meetsL[i - 1] + 1; } } meetsP[n - margP - 1] = 1; for (int i = n - margP - 2; i >= margL; i--) { if (ants[i] == ants[i + 1]) { if (ants[i] == 'P') { meetsP[i] = meetsP[i + 1]; } else { meetsP[i] = meetsP[i + 1] + 2; } } else { meetsP[i] = meetsP[i + 1] + 1; } } } for (int i = 0; i < n; i++) { meets[i] = min(meetsL[i], meetsP[i]); cout << meets[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 69 70 71 72 | #include <iostream> #include <string> using namespace std; const int MAXN = 300009; string ants; int meetsL[MAXN] = {0}; int meetsP[MAXN] = {0}; int meets[MAXN] = {0}; int main() { int n; cin >> n; cin >> ants; size_t pos_first_P = ants.find('P'); size_t pos_last_L = ants.find_last_of('L'); int margL = (pos_first_P != string::npos) ? pos_first_P : n; int margP = (pos_last_L != string::npos) ? n - pos_last_L - 1 : n; if (margL + margP < n) { meetsL[margL] = 1; for (int i = margL + 1; i < n - margP; i++) { if (ants[i] == ants[i - 1]) { if (ants[i] == 'L') { meetsL[i] = meetsL[i - 1]; } else { meetsL[i] = meetsL[i - 1] + 2; } } else { meetsL[i] = meetsL[i - 1] + 1; } } meetsP[n - margP - 1] = 1; for (int i = n - margP - 2; i >= margL; i--) { if (ants[i] == ants[i + 1]) { if (ants[i] == 'P') { meetsP[i] = meetsP[i + 1]; } else { meetsP[i] = meetsP[i + 1] + 2; } } else { meetsP[i] = meetsP[i + 1] + 1; } } } for (int i = 0; i < n; i++) { meets[i] = min(meetsL[i], meetsP[i]); cout << meets[i] << " "; } return 0; } |