Untitled
unknown
plain_text
a year ago
4.1 kB
5
Indexable
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char* readline(); char* ltrim(char*); char* rtrim(char*); char** split_string(char*); int parse_int(char*); long long calculate_health(char** genes, int* health, int n, char* dna, int first, int last) { long long strand_health = 0; for (int i = first; i <= last; i++) { char* gene = genes[i]; int gene_health = health[i]; char* match = strstr(dna, gene); while (match != NULL) { strand_health += gene_health; match = strstr(match + 1, gene); } } return strand_health; } int main() { int n = parse_int(ltrim(rtrim(readline()))); char** genes = split_string(rtrim(readline())); int* health = malloc(n * sizeof(int)); // Allocate memory for health array char** health_temp = split_string(rtrim(readline())); for (int i = 0; i < n; i++) { health[i] = parse_int(health_temp[i]); // Convert strings to integers } int s = parse_int(ltrim(rtrim(readline()))); long long max_health = LLONG_MIN; long long min_health = LLONG_MAX; for (int s_itr = 0; s_itr < s; s_itr++) { char** first_multiple_input = split_string(rtrim(readline())); int first = parse_int(first_multiple_input[0]); int last = parse_int(first_multiple_input[1]); char* dna = first_multiple_input[2]; long long strand_health = calculate_health(genes, health, n, dna, first, last); if (strand_health > max_health) { max_health = strand_health; } if (strand_health < min_health) { min_health = strand_health; } } printf("%lld %lld\n", min_health, max_health); free(health); // Free allocated memory return 0; } char* readline() { size_t alloc_length = 1024; size_t data_length = 0; char* data = malloc(alloc_length); while (true) { char* cursor = data + data_length; char* line = fgets(cursor, alloc_length - data_length, stdin); if (!line) { break; } data_length += strlen(cursor); if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } alloc_length <<= 1; data = realloc(data, alloc_length); if (!data) { data = '\0'; break; } } if (data[data_length - 1] == '\n') { data[data_length - 1] = '\0'; data = realloc(data, data_length); if (!data) { data = '\0'; } } else { data = realloc(data, data_length + 1); if (!data) { data = '\0'; } else { data[data_length] = '\0'; } } return data; } char* ltrim(char* str) { if (!str) { return '\0'; } if (!*str) { return str; } while (*str != '\0' && isspace(*str)) { str++; } return str; } char* rtrim(char* str) { if (!str) { return '\0'; } if (!*str) { return str; } char* end = str + strlen(str) - 1; while (end >= str && isspace(*end)) { end--; } *(end + 1) = '\0'; return str; } char** split_string(char* str) { char** splits = NULL; char* token = strtok(str, " "); int spaces = 0; while (token) { splits = realloc(splits, sizeof(char*) * ++spaces); if (!splits) { return splits; } splits[spaces - 1] = token; token = strtok(NULL, " "); } return splits; } int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10); if (endptr == str || *endptr != '\0') { exit(EXIT_FAILURE); } return value; }
Editor is loading...
Leave a Comment