#include <iostream> int main() { int n = 0; std::cin >> n; std::string line; std::cin >> line; if (n == 1) { std::cout << "0\n"; return 0; } else if (n == 2) { if (line[0] == 'P' && line[1] == 'L') std::cout << "1 1\n"; else std::cout << "0 0\n"; return 0; } int tab[n] = { 0 }; int left = 0, right = n - 2; while (left < right) { if (line[left] == 'P' && line[left + 1] == 'L') { line[left] = 'L'; line[left + 1] = 'P'; ++tab[left]; ++tab[left + 1]; } if (line[right] == 'P' && line[right + 1] == 'L') { line[right] = 'L'; line[right + 1] = 'P'; ++tab[right]; ++tab[right + 1]; } if (line[left] != 'L' && line[right] != 'P') { for (int i = left; i < right + 1; ++i) { if (line[i] == 'P' && line[i + 1] == 'L') { line[i] = 'L'; line[i + 1] = 'P'; ++tab[i]; ++tab[i + 1]; } } continue; } if (line[left] == 'L') ++left; if (line[right] == 'P') --right; } for (int i = 0; i < n; ++i) std::cout << tab[i] << ' '; std::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 | #include <iostream> int main() { int n = 0; std::cin >> n; std::string line; std::cin >> line; if (n == 1) { std::cout << "0\n"; return 0; } else if (n == 2) { if (line[0] == 'P' && line[1] == 'L') std::cout << "1 1\n"; else std::cout << "0 0\n"; return 0; } int tab[n] = { 0 }; int left = 0, right = n - 2; while (left < right) { if (line[left] == 'P' && line[left + 1] == 'L') { line[left] = 'L'; line[left + 1] = 'P'; ++tab[left]; ++tab[left + 1]; } if (line[right] == 'P' && line[right + 1] == 'L') { line[right] = 'L'; line[right + 1] = 'P'; ++tab[right]; ++tab[right + 1]; } if (line[left] != 'L' && line[right] != 'P') { for (int i = left; i < right + 1; ++i) { if (line[i] == 'P' && line[i + 1] == 'L') { line[i] = 'L'; line[i + 1] = 'P'; ++tab[i]; ++tab[i + 1]; } } continue; } if (line[left] == 'L') ++left; if (line[right] == 'P') --right; } for (int i = 0; i < n; ++i) std::cout << tab[i] << ' '; std::cout << '\n'; } |