-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.cc
46 lines (42 loc) · 997 Bytes
/
mastermind.cc
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
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
int findR(int length, std::string code, std::string guess) {
int r = 0;
for (int i = 0; i < length; i++)
if (code[i] == guess[i])
r++;
return r;
}
void makeAlpha(std::unordered_map<char, int>& map) {
for (int i = 0; i < map.size(); i++)
map[char(i+97)] = 0;
}
int findS(int length, int r, std::string code, std::string guess) {
int s = 0;
std::unordered_map<char, int> codeMap;
codeMap.reserve(26);
makeAlpha(codeMap);
for (int i = 0; i < length; i++) {
codeMap[code[i]]++;
}
for (int i = 0; i < length; i++) {
if (codeMap[guess[i]] > 0) {
s++;
codeMap[guess[i]]--;
}
}
return s - r;
}
int main () {
// [O(1)]
int length;
std::string code, guess;
std::cin >> length >> code >> guess;
// [2O(log n)]
int r = findR(length, code, guess);
int s = findS(length, r, code, guess);
// [O(1)]
std::cout << r << " " << s << std::endl;
}