#include <stdio.h> #include <stdlib.h> int main() { char *M; int *O,i,o,N,n; scanf("%d\n",&N); M = (char*) malloc((N+1)*sizeof(char)); //initial O = (int*) malloc(N*sizeof(int)); //ret scanf("%s",M); i = 0; o = 1; for (n = 0; n < N; n++) O[n] = 0; while (o > 0) { n = 0; o = 0; while (n+1 < N) { if (M[n] == 'P' && M[n+1] == 'L') { M[n] = 'L'; M[n+1] = 'P'; O[n]++; O[n+1]++; o++; } n++; } } for (n = 0; n < N; n++) printf("%d ",O[n]); printf("\n"); free(M); free(O); 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 42 43 | #include <stdio.h> #include <stdlib.h> int main() { char *M; int *O,i,o,N,n; scanf("%d\n",&N); M = (char*) malloc((N+1)*sizeof(char)); //initial O = (int*) malloc(N*sizeof(int)); //ret scanf("%s",M); i = 0; o = 1; for (n = 0; n < N; n++) O[n] = 0; while (o > 0) { n = 0; o = 0; while (n+1 < N) { if (M[n] == 'P' && M[n+1] == 'L') { M[n] = 'L'; M[n+1] = 'P'; O[n]++; O[n+1]++; o++; } n++; } } for (n = 0; n < N; n++) printf("%d ",O[n]); printf("\n"); free(M); free(O); return 0; } |