#include<iostream>
#include<vector>
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int n;
char x;
std::cin >> n;
std::vector<bool> w(n);
std::vector<int> res(n);
for (int i = 0; i < n; i++) {
std::cin >> x;
if (x == 'L')
w[i] = true;
else w[i] = false;
}
if (w[0] == false)
res[0] = 1;
else res[0] = 0;
for (int i = 1; i < n; i++) {
short a = 0;
if (w[i] == false) a++;
if (w[i - 1] == false) a++;
res[i] = res[i - 1] + a;
}
int pomocnik = 0;
if (w[n - 1] == true)
pomocnik = 1;
else pomocnik = 0;
if (pomocnik < res[n - 1]) {
res[n - 1] = pomocnik;
for (int i = n - 2; i >= 0; i--) {
if (w[i] == true) pomocnik++;
if (w[i + 1] == true) pomocnik++;
if (pomocnik < res[i]) {
res[i] = pomocnik;
}
else
break;
}
}
for (int i = 0; i < n; i++) {
std::cout << res[i] << " ";
}
}
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 | #include<iostream> #include<vector> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n; char x; std::cin >> n; std::vector<bool> w(n); std::vector<int> res(n); for (int i = 0; i < n; i++) { std::cin >> x; if (x == 'L') w[i] = true; else w[i] = false; } if (w[0] == false) res[0] = 1; else res[0] = 0; for (int i = 1; i < n; i++) { short a = 0; if (w[i] == false) a++; if (w[i - 1] == false) a++; res[i] = res[i - 1] + a; } int pomocnik = 0; if (w[n - 1] == true) pomocnik = 1; else pomocnik = 0; if (pomocnik < res[n - 1]) { res[n - 1] = pomocnik; for (int i = n - 2; i >= 0; i--) { if (w[i] == true) pomocnik++; if (w[i + 1] == true) pomocnik++; if (pomocnik < res[i]) { res[i] = pomocnik; } else break; } } for (int i = 0; i < n; i++) { std::cout << res[i] << " "; } } |
English