#include <iostream> #include <algorithm> using namespace std; int main() { int **tab; char *ants; int n; cin >> n; tab = new int*[n]; ants = new char[n]; for(int i=0; i<n; i++){ tab[i] = new int[2]; cin >> ants[i]; } //run sweep from left to right int pFromL = 0; for(int i=0; i<n; i++){ tab[i][0] = pFromL; if(ants[i] == 'P') { pFromL++; } } //run sweep from right to left int lFromP = 0; for(int i = n-1; i >= 0; i--) { tab[i][1] = lFromP; if(ants[i] == 'L') { lFromP++; } } //combine and print results for(int i=0; i<n; i++) { int overhang = 0; if((ants[i] == 'P' && tab[i][0] < tab[i][1]) || (ants[i] == 'L' && tab[i][0] > tab[i][1])) { overhang = 1; } cout << min(tab[i][0], tab[i][1]) * 2 + overhang << " "; } cout << endl; delete tab; delete ants; 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 <algorithm> using namespace std; int main() { int **tab; char *ants; int n; cin >> n; tab = new int*[n]; ants = new char[n]; for(int i=0; i<n; i++){ tab[i] = new int[2]; cin >> ants[i]; } //run sweep from left to right int pFromL = 0; for(int i=0; i<n; i++){ tab[i][0] = pFromL; if(ants[i] == 'P') { pFromL++; } } //run sweep from right to left int lFromP = 0; for(int i = n-1; i >= 0; i--) { tab[i][1] = lFromP; if(ants[i] == 'L') { lFromP++; } } //combine and print results for(int i=0; i<n; i++) { int overhang = 0; if((ants[i] == 'P' && tab[i][0] < tab[i][1]) || (ants[i] == 'L' && tab[i][0] > tab[i][1])) { overhang = 1; } cout << min(tab[i][0], tab[i][1]) * 2 + overhang << " "; } cout << endl; delete tab; delete ants; return 0; } |