#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #define LEFT 'L' #define RIGHT 'P' #define BOOL char #define TRUE 1 #define FALSE 0 int main(void) { uint_fast32_t n; register uint_fast32_t off_l, off_r, last_col; register uint_fast32_t *count; register char *str; /* Input: */ scanf("%u\n", &n); str = (char *) malloc(n + 1); count = (uint_fast32_t *)calloc(n, sizeof(uint_fast32_t)); off_l = 0; off_r = (n - 1); fgets(str, n + 1, stdin); //Main loop: while (off_l < off_r) { register uint_fast32_t k; BOOL start_l = TRUE; last_col = off_r; // printf_s("%.*s\n", off_r - off_l + 1, str + off_l); for (k = off_l; k < off_r; ++k) { //Getting rid of leading L's: if (str[k] == RIGHT) { if(start_l) off_l = k; start_l = FALSE; } //Collision: if yes: if (str[k + 1] == LEFT && str[k] == RIGHT) { str[k + 1] = RIGHT; str[k] = LEFT; ++count[k + 1]; ++count[k]; last_col = k; } } off_r = last_col; if (start_l) break; } //Output: for (int i = 0; i < n; ++i) { fprintf(stdout, "%u ", count[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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #define LEFT 'L' #define RIGHT 'P' #define BOOL char #define TRUE 1 #define FALSE 0 int main(void) { uint_fast32_t n; register uint_fast32_t off_l, off_r, last_col; register uint_fast32_t *count; register char *str; /* Input: */ scanf("%u\n", &n); str = (char *) malloc(n + 1); count = (uint_fast32_t *)calloc(n, sizeof(uint_fast32_t)); off_l = 0; off_r = (n - 1); fgets(str, n + 1, stdin); //Main loop: while (off_l < off_r) { register uint_fast32_t k; BOOL start_l = TRUE; last_col = off_r; // printf_s("%.*s\n", off_r - off_l + 1, str + off_l); for (k = off_l; k < off_r; ++k) { //Getting rid of leading L's: if (str[k] == RIGHT) { if(start_l) off_l = k; start_l = FALSE; } //Collision: if yes: if (str[k + 1] == LEFT && str[k] == RIGHT) { str[k + 1] = RIGHT; str[k] = LEFT; ++count[k + 1]; ++count[k]; last_col = k; } } off_r = last_col; if (start_l) break; } //Output: for (int i = 0; i < n; ++i) { fprintf(stdout, "%u ", count[i]); } return 0; } |