#include <bits/stdc++.h> using namespace std; int amount; char directions[300000]; int prefixes[300000]; int suffixes[300000]; int main() { ios_base::sync_with_stdio(true); cin.tie(0); cin >> amount; for (int i = 0; i < amount; ++i) { cin >> directions[i]; } if (directions[0] == 'P') prefixes[0] = 1; for (int i = 1; i < amount; ++i) { if (directions[i] == 'P') { if (directions[i - 1] == 'P') prefixes[i] = prefixes[i - 1] + 2; else prefixes[i] = prefixes[i - 1] + 1; } else { if (directions[i - 1] == 'P') prefixes[i] = prefixes[i - 1] + 1; else prefixes[i] = prefixes[i - 1]; } } if (directions[amount - 1] == 'L') suffixes[amount - 1] = 1; for (int i = amount - 2; i >= 0; --i) { if (directions[i] == 'L') { if (directions[i + 1] == 'L') suffixes[i] = suffixes[i + 1] + 2; else suffixes[i] = suffixes[i + 1] + 1; } else { if (directions[i + 1] == 'L') suffixes[i] = suffixes[i + 1] + 1; else suffixes[i] = suffixes[i + 1]; } } for (int i = 0; i < amount; ++i) { cout << min(prefixes[i], suffixes[i]) << ' '; } cout << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; int amount; char directions[300000]; int prefixes[300000]; int suffixes[300000]; int main() { ios_base::sync_with_stdio(true); cin.tie(0); cin >> amount; for (int i = 0; i < amount; ++i) { cin >> directions[i]; } if (directions[0] == 'P') prefixes[0] = 1; for (int i = 1; i < amount; ++i) { if (directions[i] == 'P') { if (directions[i - 1] == 'P') prefixes[i] = prefixes[i - 1] + 2; else prefixes[i] = prefixes[i - 1] + 1; } else { if (directions[i - 1] == 'P') prefixes[i] = prefixes[i - 1] + 1; else prefixes[i] = prefixes[i - 1]; } } if (directions[amount - 1] == 'L') suffixes[amount - 1] = 1; for (int i = amount - 2; i >= 0; --i) { if (directions[i] == 'L') { if (directions[i + 1] == 'L') suffixes[i] = suffixes[i + 1] + 2; else suffixes[i] = suffixes[i + 1] + 1; } else { if (directions[i + 1] == 'L') suffixes[i] = suffixes[i + 1] + 1; else suffixes[i] = suffixes[i + 1]; } } for (int i = 0; i < amount; ++i) { cout << min(prefixes[i], suffixes[i]) << ' '; } cout << '\n'; } |