#include #include int collapse(char *in, char *out, char skip) { #define CASE_BIT ('A' ^ 'a') int c; char *end = out - 1; while (c = *in++) { if ((c ^ skip) & ~CASE_BIT) { if (end >= out && (c ^ CASE_BIT) == *end) --end; else *++end = c; } } *++end = '\0'; return end - out; } int main(int argc, char *argv[]) { #define IO_ERROR 2 #define INPUT_TOO_LONG 1 #define SUCCESS 0 #define MAX_LENGTH 65536 static char polymer[MAX_LENGTH], collapsed[MAX_LENGTH]; int length, shortest, skip, status = IO_ERROR; FILE *input; if (input = fopen("input.txt", "r")) { if (fgets(polymer, MAX_LENGTH, input)) { status = INPUT_TOO_LONG; length = strlen(polymer) - 1; if (length >= 0 && polymer[length] == '\n') { status = SUCCESS; polymer[length] = '\0'; shortest = length; for (skip = 'a'; skip <= 'z'; ++skip) { length = collapse(polymer, collapsed, skip); if (shortest > length) shortest = length; } printf("%d\n", shortest); } } fclose(input); } return status; }