#include <iostream> using namespace std; const int MX = 3e5+3; int n; string s; int left_dp[MX], right_dp[MX]; int main(){ ios::sync_with_stdio(false); cin >> n >> s; int ans = 0; char last = 'L'; for(int i = 0; i < n; ++i){ if(s[i] != last) ans += 1; else if (s[i] == 'P') ans += 2; last = s[i]; left_dp[i] = ans; } ans = 0; last = 'P'; for(int i = n-1; i >= 0; --i){ if(s[i] != last) ans += 1; else if (s[i] == 'L') ans += 2; last = s[i]; right_dp[i] = ans; } for(int i = 0; i < n; ++ i){ cout << min(left_dp[i], right_dp[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 | #include <iostream> using namespace std; const int MX = 3e5+3; int n; string s; int left_dp[MX], right_dp[MX]; int main(){ ios::sync_with_stdio(false); cin >> n >> s; int ans = 0; char last = 'L'; for(int i = 0; i < n; ++i){ if(s[i] != last) ans += 1; else if (s[i] == 'P') ans += 2; last = s[i]; left_dp[i] = ans; } ans = 0; last = 'P'; for(int i = n-1; i >= 0; --i){ if(s[i] != last) ans += 1; else if (s[i] == 'L') ans += 2; last = s[i]; right_dp[i] = ans; } for(int i = 0; i < n; ++ i){ cout << min(left_dp[i], right_dp[i]) << " "; } return 0; } |