#include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define INT(x) int x; scanf("%d", &x) #define LINE(n,x) char x[n]; fgets(x, n, stdin) int cl[300001], cp[300001]; int main() { INT(n); scanf("\n"); LINE(300030,a); cl[n] = 0; REPD(i,n) cl[i] = cl[i + 1] + (a[i] == 'L'); cp[0] = 0; REP(i,n) cp[i + 1] = cp[i] + (a[i] == 'P'); REP(i,n) { if (i) printf(" "); int b1 = cp[i], b2 = cl[i + 1]; if (a[i] == 'L') { b1 = min(b1, b2 + 1); b2 = min(b2, b1); } else { b2 = min(b2, b1 + 1); b1 = min(b1, b2); } printf("%d", b1 + b2); } printf("\n"); }
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 | #include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define INT(x) int x; scanf("%d", &x) #define LINE(n,x) char x[n]; fgets(x, n, stdin) int cl[300001], cp[300001]; int main() { INT(n); scanf("\n"); LINE(300030,a); cl[n] = 0; REPD(i,n) cl[i] = cl[i + 1] + (a[i] == 'L'); cp[0] = 0; REP(i,n) cp[i + 1] = cp[i] + (a[i] == 'P'); REP(i,n) { if (i) printf(" "); int b1 = cp[i], b2 = cl[i + 1]; if (a[i] == 'L') { b1 = min(b1, b2 + 1); b2 = min(b2, b1); } else { b2 = min(b2, b1 + 1); b1 = min(b1, b2); } printf("%d", b1 + b2); } printf("\n"); } |