-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask14b.cpp
More file actions
115 lines (101 loc) · 2.13 KB
/
task14b.cpp
File metadata and controls
115 lines (101 loc) · 2.13 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
string line, cur;
getline(cin, line);
unordered_map<string, ll> count;
for (int i = 0; i < line.size(); ++i) {
if (i != line.size() - 1) {
cur = line.substr(i, 2);
count[cur]++;
}
}
string two, insert;
vector<pair<string, string>> inserts;
while (getline(cin, cur)) {
if (cur.find("->") != string::npos) {
two = cur.substr(0, 2);
insert = cur.back();
inserts.push_back(make_pair(two, insert));
}
}
ll curCount;
for (int t = 0; t < 40; ++t) {
unordered_map<string, ll> tempCount;
for (auto &[two, insert] : inserts) {
if (count.find(two) != count.end()) {
curCount = count[two];
count[two] = 0;
tempCount[two[0] + insert] += curCount;
tempCount[insert + two[1]] += curCount;
}
}
for (auto &[k, v]: tempCount) {
count[k] += v;
}
}
unordered_map<char, ll> alphabetCount;
for (auto &[k, v]: count) {
alphabetCount[k[0]] += v;
alphabetCount[k[1]] += v;
//cout << k[0] << ' ' << k[1] << ' ' << v << '\n';
}
ll maxCount = 0, minCount = 1e18;
int numOdd = 0;
for (auto &[k, v]: alphabetCount) {
if (v % 2 == 1) {
v /= 2;
v++;
numOdd++;
} else {
v /= 2;
}
maxCount = max(maxCount, v);
minCount = min(minCount, v);
}
if (numOdd == 0) { // line[0] == line.back()
alphabetCount[line[0]]++;
maxCount = 0, minCount = 1e18;
for (auto &[k, v] : alphabetCount) {
maxCount = max(maxCount, v);
minCount = min(minCount, v);
}
}
cout << maxCount - minCount;
return 0;
}
/*
NBCCNBBBCBHCB
BH 1
CC 1
HC 1
BB 2
CN 1
CB 2
BC 2
NB 2
B 11 = 6
H 2 = 1
C 8 = 4
N 3 = 2
*/
/*
ABABC
AB
BA
AB
BC
A 3
B 4
C 1
ABBCA
AB
BB
BC
CA
A 2 = 1
B 4 = 2
C 2 = 1
*/