#include <iostream> using namespace std; int n, lefts, rights, s; char m; int main() { cin >> n; int ants[n], score[n]; for(int i = 0; i < n; i++) { cin >> m; if(m == 'L') { ants[i] = 0; lefts++; } else { ants[i] = 1; rights++; } } if(lefts > 0) { if(ants[0] == 1) s = 1; score[0] = s; for(int i = 1; i < lefts; i++) { if(ants[i] == ants[i-1]) { if(ants[i] == 1) s += 2; } else { s += 1; } score[i] = s; } } if(rights > 0) { if(ants[n-1] == 0) {s = 1;} else {s = 0;} score[n-1] = s; for(int j = n-2; j >= n-rights; j--) { if(ants[j] == ants[j+1]) { if(ants[j] == 0) s += 2; } else { s += 1; } score[j] = s; } } for(int i = 0; i < n; i++) { cout << score[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 | #include <iostream> using namespace std; int n, lefts, rights, s; char m; int main() { cin >> n; int ants[n], score[n]; for(int i = 0; i < n; i++) { cin >> m; if(m == 'L') { ants[i] = 0; lefts++; } else { ants[i] = 1; rights++; } } if(lefts > 0) { if(ants[0] == 1) s = 1; score[0] = s; for(int i = 1; i < lefts; i++) { if(ants[i] == ants[i-1]) { if(ants[i] == 1) s += 2; } else { s += 1; } score[i] = s; } } if(rights > 0) { if(ants[n-1] == 0) {s = 1;} else {s = 0;} score[n-1] = s; for(int j = n-2; j >= n-rights; j--) { if(ants[j] == ants[j+1]) { if(ants[j] == 0) s += 2; } else { s += 1; } score[j] = s; } } for(int i = 0; i < n; i++) { cout << score[i] << " "; } return 0; } |