-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (155 loc) · 4.07 KB
/
main.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Daniil Zimin
// tg: daniilzimin4
#include <bits/stdc++.h>
#include <utility>
using namespace std;
typedef long long ll;
typedef long double ld;
mt19937 rnd(time(0));
#define ioss ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("sum.in", "r", stdin); freopen("sum.out", "w", stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
template<class T>
ll upmax(T &a, T b) { return (b > a) ? a = b, 1 : 0; }
template<class T>
ll upmin(T &a, T b) { return (b < a) ? a = b, 1 : 0; }
const ll inf = 1e18 + 7;
template<class K, class V>
struct MyHashMap {
int n = 2;
int maxLoadFactor = 2;
int now = 0;
struct Bucket {
K key;
V value;
Bucket(K k, V v) : key(std::move(k)), value(v) {};
int hash(int mod) {
int local = 0;
if (typeid(key) == typeid(string)) {
int prime = 137;
for (auto i: key) {
local = (local * prime) + i;
local %= mod;
}
}
return local;
}
bool operator==(const Bucket &o) {
return (key == o.key);
}
};
vector<vector<Bucket>> list = vector<vector<Bucket>>(n, vector<Bucket>());
private:
void reset() {
n *= 2;
now = 0;
vector<vector<Bucket>> lastList = list;
list.clear();
list.resize(n * 2, vector<Bucket>());
for (int i = 0; i < lastList.size(); i += 1) {
for (const auto &j: lastList[i]) {
add_(j);
}
}
}
void add_(Bucket el) {
int hash = el.hash(n);
for (int i = 0; i < list[hash].size(); i += 1) {
if (el == list[hash][i]) {
list[hash][i].value += el.value;
return;
}
}
list[hash].pb(el);
now += 1;
if (now > maxLoadFactor * n) {
reset();
}
}
public:
void addKeyAndValue(K key, V value) {
add_(Bucket(key, value));
}
void addOnlyKey(K key) {
add_(Bucket(key, 1));
}
vector<pair<V, K>> convertToArr() {
vector<pair<V, K>> arr;
for (int i = 0; i < n; i += 1) {
for (const auto &el: list[i]) {
arr.push_back({el.value, el.key});
}
}
return arr;
}
bool contains(K key) {
Bucket bucket(key, 1);
int hash = bucket.hash(n);
for (const auto &i: list[hash]) {
if (i.key == bucket.key) {
return true;
}
}
return false;
}
void removeFull(K key) {
Bucket bucket(key, 1);
int hash = bucket.hash(n);
for (int i = 0; i < list[hash]; i += 1) {
if (bucket.key == list[hash][i].key) {
list[hash].erase(list[hash].begin() + i);
return;
}
}
}
};
void n2sort(vector<pair<int, string>> &a) {
for (int i = 0; i < a.size(); i += 1) {
for (int j = 0; j < a.size() - 1; j += 1) {
if (a[j].first < a[j + 1].first || (a[j].first == a[j + 1].first && a[j].second > a[j + 1].second)) {
swap(a[j], a[j + 1]);
}
}
}
}
void solve() {
MyHashMap<string, int> MyMap;
int n;
cin >> n;
for (int i = 0; i < n; i += 1) {
string x;
cin >> x;
MyMap.addOnlyKey(x);
}
vector<string> ans;
int m;
cin >> m;
MyHashMap<string, int> MyMap2;
for (int i = 0; i < m; i += 1) {
string x;
cin >> x;
if (!MyMap.contains(x) && !MyMap2.contains(x)) {
ans.pb(x);
MyMap2.addOnlyKey(x);
}
}
cout << ans.size() << '\n';
for (auto i: ans) {
cout << i << '\n';
}
}
signed main() {
ioss
// file
// (a += b) -> return value
int t = 1;
// cin >> t;
while (t--) {
solve();
}
// printf("Time taken: %.10fs\n", (double)(clock() - CLOCK_START)/CLOCKS_PER_SEC);
return 0;
}