#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; string s; cin >> s; s = '+' + s; vector <int> L(n+2), P(n+2); for(int i = 1; i<=n; ++i){ L[i] = L[i-1] + (L[i-1] % 2) + (s[i] == 'P' ? 1 : 0); } for(int i = n; i>=1; --i){ P[i] = P[i+1] + (P[i+1] % 2) + (s[i] == 'L' ? 1 : 0); } for(int i = 1; i<=n; ++i){ cout << min(L[i], P[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 | #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; string s; cin >> s; s = '+' + s; vector <int> L(n+2), P(n+2); for(int i = 1; i<=n; ++i){ L[i] = L[i-1] + (L[i-1] % 2) + (s[i] == 'P' ? 1 : 0); } for(int i = n; i>=1; --i){ P[i] = P[i+1] + (P[i+1] % 2) + (s[i] == 'L' ? 1 : 0); } for(int i = 1; i<=n; ++i){ cout << min(L[i], P[i]) << " "; } } |