#include <iostream> using namespace std; int main() { int n; scanf("%d", &n); char a[n]; int prawo[n]; int lewo[n]; getchar(); for (int i=0; i<n; i++) a[i] = getchar(); if (a[0]=='L') prawo[0] = 0; else prawo[0] = 1; if (a[n-1]=='P') lewo[n-1] = 0; else lewo[n-1] = 1; for (int i=1; i<n; i++) { if (a[i] == 'L' && prawo[i-1] == 0) prawo[i] = prawo[i-1]; else if (a[i-1] == a[i] && a[i]=='L') prawo[i] = prawo[i-1]; else if (a[i-1] == a[i]) prawo[i] = prawo[i-1] + 2; else prawo[i] = prawo[i-1] + 1; } for (int i=n-2; i>=0; i--) { if (a[i] == 'P' && lewo[i+1] == 0) lewo[i] = lewo[i+1]; else if (a[i+1] == a[i] && a[i] == 'P') lewo[i] = lewo[i+1]; else if (a[i+1] == a[i]) lewo[i] = lewo[i+1] + 2; else lewo[i] = lewo[i+1] + 1; } for (int i=0; i<n; i++) { if (prawo[i] < lewo[i]) printf("%d ", prawo[i]); else printf("%d ", lewo[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 | #include <iostream> using namespace std; int main() { int n; scanf("%d", &n); char a[n]; int prawo[n]; int lewo[n]; getchar(); for (int i=0; i<n; i++) a[i] = getchar(); if (a[0]=='L') prawo[0] = 0; else prawo[0] = 1; if (a[n-1]=='P') lewo[n-1] = 0; else lewo[n-1] = 1; for (int i=1; i<n; i++) { if (a[i] == 'L' && prawo[i-1] == 0) prawo[i] = prawo[i-1]; else if (a[i-1] == a[i] && a[i]=='L') prawo[i] = prawo[i-1]; else if (a[i-1] == a[i]) prawo[i] = prawo[i-1] + 2; else prawo[i] = prawo[i-1] + 1; } for (int i=n-2; i>=0; i--) { if (a[i] == 'P' && lewo[i+1] == 0) lewo[i] = lewo[i+1]; else if (a[i+1] == a[i] && a[i] == 'P') lewo[i] = lewo[i+1]; else if (a[i+1] == a[i]) lewo[i] = lewo[i+1] + 2; else lewo[i] = lewo[i+1] + 1; } for (int i=0; i<n; i++) { if (prawo[i] < lewo[i]) printf("%d ", prawo[i]); else printf("%d ", lewo[i]); } return 0; } |