blob: a13142ef4b96519ad9a5a836aa6bcaf73b5c3070 (
plain) (
blame)
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
|
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct {
int a, b, c;
} rules[128];
static size_t len;
static size_t freq[26];
static struct Pairs {
size_t _[26][26];
} pairs;
static void step(void) {
struct Pairs next = pairs;
for (size_t i = 0; i < len; ++i) {
size_t n = pairs._[rules[i].a][rules[i].b];
next._[rules[i].a][rules[i].c] += n;
next._[rules[i].c][rules[i].b] += n;
next._[rules[i].a][rules[i].b] -= n;
freq[rules[i].c] += n;
}
pairs = next;
}
int main(void) {
size_t cap = 0;
char *buf = NULL;
ssize_t n = getline(&buf, &cap, stdin);
if (buf[n-1] == '\n') buf[n-1] = '\0';
freq[buf[0]-'A']++;
for (size_t i = 0; buf[i+1]; ++i) {
freq[buf[i+1]-'A']++;
pairs._[buf[i]-'A'][buf[i+1]-'A']++;
}
char a, b, c;
while (EOF != scanf(" %c%c -> %c\n", &a, &b, &c)) {
rules[len].a = a - 'A';
rules[len].b = b - 'A';
rules[len].c = c - 'A';
len++;
}
for (int i = 0; i < 10; ++i) {
step();
}
size_t min = SIZE_MAX, max = 0;
for (int i = 0; i < 26; ++i) {
if (freq[i] && freq[i] < min) min = freq[i];
if (freq[i] > max) max = freq[i];
}
printf("%zu\n", max - min);
for (int i = 0; i < 30; ++i) {
step();
}
min = SIZE_MAX;
max = 0;
for (int i = 0; i < 26; ++i) {
if (freq[i] && freq[i] < min) min = freq[i];
if (freq[i] > max) max = freq[i];
}
printf("%zu\n", max - min);
}
|