#include <bits/stdc++.h>
using namespace std;
long long res[300005];
int main(){
int n;
cin>>n;
string s;
cin>>s;
queue<int> Q;
int cnt = 0;
for(int i=0;i<n;i++){
if(s[i] == 'P'){
Q.push(i);
res[i] -= cnt;
}
else{
if(Q.empty()){
continue;
}
int j = Q.front();
Q.pop();
res[j] += cnt+1;
cnt += 2;
res[i] -= cnt;
res[i]++;
Q.push(i);
}
}
while(!Q.empty()){
int u = Q.front();
Q.pop();
res[u] += cnt;
}
for(int i=0;i<n;i++) cout<<res[i]<<" ";
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 | #include <bits/stdc++.h> using namespace std; long long res[300005]; int main(){ int n; cin>>n; string s; cin>>s; queue<int> Q; int cnt = 0; for(int i=0;i<n;i++){ if(s[i] == 'P'){ Q.push(i); res[i] -= cnt; } else{ if(Q.empty()){ continue; } int j = Q.front(); Q.pop(); res[j] += cnt+1; cnt += 2; res[i] -= cnt; res[i]++; Q.push(i); } } while(!Q.empty()){ int u = Q.front(); Q.pop(); res[u] += cnt; } for(int i=0;i<n;i++) cout<<res[i]<<" "; cout<<endl; } |
English