|
| 1 | +#include <assert.h> |
| 2 | +#include <ctype.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <math.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +char* readline(); |
| 13 | +char* ltrim(char*); |
| 14 | +char* rtrim(char*); |
| 15 | + |
| 16 | +long parse_long(char*); |
| 17 | + |
| 18 | +/* |
| 19 | + * Complete the 'repeatedString' function below. |
| 20 | + * |
| 21 | + * The function is expected to return a LONG_INTEGER. |
| 22 | + * The function accepts following parameters: |
| 23 | + * 1. STRING s |
| 24 | + * 2. LONG_INTEGER n |
| 25 | + */ |
| 26 | + |
| 27 | +long repeatedString(char* s, long n) { |
| 28 | + long string_length = strlen(s); // Length of the string s |
| 29 | + long full_repeats = n / string_length; // Full repetitions of the string s |
| 30 | + long remainder = n % string_length; // Remaining characters after full repetitions |
| 31 | + |
| 32 | + // Count 'a' in the original string |
| 33 | + long a_count = 0; |
| 34 | + for (long i = 0; i < string_length; i++) { |
| 35 | + if (s[i] == 'a') { |
| 36 | + a_count++; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Count 'a' in the remainder substring |
| 41 | + long remainder_a_count = 0; |
| 42 | + for (long i = 0; i < remainder; i++) { |
| 43 | + if (s[i] == 'a') { |
| 44 | + remainder_a_count++; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Total count of 'a' |
| 49 | + return (full_repeats * a_count) + remainder_a_count; |
| 50 | +} |
| 51 | + |
| 52 | +int main() |
| 53 | +{ |
| 54 | + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); |
| 55 | + |
| 56 | + char* s = readline(); |
| 57 | + |
| 58 | + long n = parse_long(ltrim(rtrim(readline()))); |
| 59 | + |
| 60 | + long result = repeatedString(s, n); |
| 61 | + |
| 62 | + fprintf(fptr, "%ld\n", result); |
| 63 | + |
| 64 | + fclose(fptr); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +char* readline() { |
| 70 | + size_t alloc_length = 1024; |
| 71 | + size_t data_length = 0; |
| 72 | + |
| 73 | + char* data = malloc(alloc_length); |
| 74 | + |
| 75 | + while (true) { |
| 76 | + char* cursor = data + data_length; |
| 77 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 78 | + |
| 79 | + if (!line) { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + data_length += strlen(cursor); |
| 84 | + |
| 85 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + alloc_length <<= 1; |
| 90 | + |
| 91 | + data = realloc(data, alloc_length); |
| 92 | + |
| 93 | + if (!data) { |
| 94 | + data = '\0'; |
| 95 | + |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (data[data_length - 1] == '\n') { |
| 101 | + data[data_length - 1] = '\0'; |
| 102 | + |
| 103 | + data = realloc(data, data_length); |
| 104 | + |
| 105 | + if (!data) { |
| 106 | + data = '\0'; |
| 107 | + } |
| 108 | + } else { |
| 109 | + data = realloc(data, data_length + 1); |
| 110 | + |
| 111 | + if (!data) { |
| 112 | + data = '\0'; |
| 113 | + } else { |
| 114 | + data[data_length] = '\0'; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return data; |
| 119 | +} |
| 120 | + |
| 121 | +char* ltrim(char* str) { |
| 122 | + if (!str) { |
| 123 | + return '\0'; |
| 124 | + } |
| 125 | + |
| 126 | + if (!*str) { |
| 127 | + return str; |
| 128 | + } |
| 129 | + |
| 130 | + while (*str != '\0' && isspace(*str)) { |
| 131 | + str++; |
| 132 | + } |
| 133 | + |
| 134 | + return str; |
| 135 | +} |
| 136 | + |
| 137 | +char* rtrim(char* str) { |
| 138 | + if (!str) { |
| 139 | + return '\0'; |
| 140 | + } |
| 141 | + |
| 142 | + if (!*str) { |
| 143 | + return str; |
| 144 | + } |
| 145 | + |
| 146 | + char* end = str + strlen(str) - 1; |
| 147 | + |
| 148 | + while (end >= str && isspace(*end)) { |
| 149 | + end--; |
| 150 | + } |
| 151 | + |
| 152 | + *(end + 1) = '\0'; |
| 153 | + |
| 154 | + return str; |
| 155 | +} |
| 156 | + |
| 157 | +long parse_long(char* str) { |
| 158 | + char* endptr; |
| 159 | + long value = strtol(str, &endptr, 10); |
| 160 | + |
| 161 | + if (endptr == str || *endptr != '\0') { |
| 162 | + exit(EXIT_FAILURE); |
| 163 | + } |
| 164 | + |
| 165 | + return value; |
| 166 | +} |
0 commit comments