#include <bits/stdc++.h> using namespace std; typedef long long ll; #define st first; #define nd second; int main() { ios_base::sync_with_stdio(false);cin.tie(0); int n; cin>>n; string s; cin>>s; vector<int>a(n+1); vector<int>b(n+1); for(int i=1;i<=n;i++) { a[i]=a[i-1]; if(s[i-1]=='P') { a[i]++; } } for(int i=n-1;i>=0;i--) { b[i]=b[i+1]; if(s[i]=='L') { b[i]++; } } for(int i=0;i<n;i++) { if(s[i]=='L') { cout<<min(a[i],b[i+1])*2+(a[i]>b[i+1])<<" "; } else { cout<<min(a[i],b[i+1])*2+(a[i]<b[i+1])<<" "; } } cout<<endl; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define st first; #define nd second; int main() { ios_base::sync_with_stdio(false);cin.tie(0); int n; cin>>n; string s; cin>>s; vector<int>a(n+1); vector<int>b(n+1); for(int i=1;i<=n;i++) { a[i]=a[i-1]; if(s[i-1]=='P') { a[i]++; } } for(int i=n-1;i>=0;i--) { b[i]=b[i+1]; if(s[i]=='L') { b[i]++; } } for(int i=0;i<n;i++) { if(s[i]=='L') { cout<<min(a[i],b[i+1])*2+(a[i]>b[i+1])<<" "; } else { cout<<min(a[i],b[i+1])*2+(a[i]<b[i+1])<<" "; } } cout<<endl; } |