-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathR2_C.cpp
84 lines (78 loc) · 1.45 KB
/
R2_C.cpp
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
// Google Code Jam 2015 R2
// Problem C.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<int> IntVec;
typedef map<string, int> StrIntMap;
int lang[2][20001];
IntVec v[20];
int solve(LL N) {
int ans = 0;
for (int i = 0; i <= 20000; ++i) {
ans += lang[0][i] && lang[1][i];
}
int M = 1 << (N - 2);
if (M >= 2) {
ans = 1 << 20;
for (int b = 0; b < M; ++b) {
int temp = 0;
int l[2][20001];
memcpy(l, lang, sizeof(l));
for (int i = 0; i < N - 2; ++i) {
int d = ((1 << i) & b) == 0;
for (int a : v[i + 2]) {
l[d][a] = 1;
}
}
for (int i = 0; i <= 20000; ++i) {
temp += l[0][i] && l[1][i];
}
ans = min(ans, temp);
}
}
return ans;
}
int main(int argc, char *argv[]) {
string s;
getline(cin, s);
LL T = atoi(s.c_str());
for (LL t = 1; t <= T; ++t) {
memset(lang, 0, sizeof(lang));
int cnt = 0;
getline(cin, s);
int N = atoi(s.c_str());
StrIntMap kw;
for (LL i = 0; i < N; ++i) {
v[i].clear();
getline(cin, s);
stringstream ss(s);
while (true) {
string k;
ss >> k;
if (k.empty()) {
break;
}
int idx;
if (kw.count(k) <= 0) {
idx = cnt;
kw[k] = cnt;
++cnt;
} else {
idx = kw[k];
}
if (i <= 1) {
lang[i][idx] = 1;
}
v[i].push_back(idx);
}
}
cout << "Case #" << t << ": " << solve(N) << endl;
}
return 0;
}