-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
52 lines (45 loc) · 1 KB
/
main.cpp
File metadata and controls
52 lines (45 loc) · 1 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
#include <iostream>
#include <unordered_map>
#include <set>
#include <string>
struct Pair {
int h;
int p;
Pair(int hits, int pseudo) {
h = hits;
p = pseudo;
}
};
Pair mastermind(const std::string& query, const std::string& solution) {
int hits = 0;
int pseudo = 0;
std::unordered_map<char, int> map;
int i = 0;
for (const auto& c: solution) {
map[c] = i;
i++;
}
std::set<char> found;
i = 0;
for (const auto& c: query) {
if (map.count(c)) {
if (map[c] == i) {
hits++;
found.insert(c);
}
}
i++;
}
for (const auto& c: query) {
if ((solution.find(c) != std::string::npos) && !found.count(c)) {
pseudo++;
found.insert(c);
}
}
return Pair(hits, pseudo);
}
int main() {
auto result = mastermind("RGBY", "GGRR");
std::cout << result.h << " " << result.p << std::endl;
return 0;
}