6 struct template { char x1
; char x2
; char ins
; };
8 #define lookup(i, x1, x2) (i)[((x1)-'A')+((x2)-'A')*26]
15 struct template template[100];
16 unsigned long input1
[26*26] = {0}, input2
[26*26] = {0};
17 unsigned long *input
= input1
, *newinput
= input2
;
19 int first
= getchar();
21 unsigned long freq
[26] = {0};
22 while ( (c
= getchar()) != '\n') {
23 lookup(input
, last
, c
) = 1;
27 while (getline(&buf
, &len
, stdin
) != -1) {
30 template[ntemplate
++] = (struct template)
31 {.x1
=buf
[0], .x2
=buf
[1], .ins
=buf
[6]};
34 for (int step
= 0; step
< 40; step
++) {
35 for (int i
= 0; i
<ntemplate
; i
++) {
36 struct template t
= template[i
];
37 unsigned long freq
= lookup(input
, t
.x1
, t
.x2
);
38 lookup(newinput
, t
.x1
, t
.ins
) += freq
;
39 lookup(newinput
, t
.ins
, t
.x2
) += freq
;
41 unsigned long *t
= newinput
;
44 memset(&newinput
[0], 0, sizeof(input1
));
47 for (char x
= 'A'; x
<='Z'; x
++) {
48 for (char y
= 'A'; y
<='Z'; y
++) {
49 freq
[x
-'A'] += lookup(input
, x
, y
);
50 freq
[y
-'A'] += lookup(input
, x
, y
);
56 unsigned long min
= ULONG_MAX
, max
= 0;
57 for (int i
= 0; i
<26; i
++) {
58 max
= freq
[i
] > max
? freq
[i
] : max
;
59 min
= freq
[i
] > 1 && freq
[i
] < min
? freq
[i
] : min
;
62 printf("%lu\n", max
/2-min
/2);