forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.5.cpp
60 lines (57 loc) · 1.53 KB
/
19.5.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
#include <iostream>
#include <cstring>
using namespace std;
struct Result{
int hits;
int pseudo_hits;
};
Result Estimate(const char* solution, const char* guess){
Result res;
res.hits = 0;
res.pseudo_hits = 0;
int solution_mask = 0;
for(int i=0; i<4; ++i){
solution_mask |= 1 << (solution[i] - 'A');
}
for(int i=0; i<4; ++i){
if(guess[i] == solution[i])
++res.hits;
else if(solution_mask & ( 1<<(guess[i] - 'A')))
++res.pseudo_hits;
}
return res;
}
int Min(int a, int b){
return a < b ? a : b;
}
Result Estimate1(const char* solution, const char* guess){
Result res;
res.hits = 0;
res.pseudo_hits = 0;
int num = 26 + 5;
int guess_count[num], solution_count[num];
memset(guess_count, 0, sizeof(guess_count));
memset(solution_count, 0, sizeof(solution_count));
for(int i=0; i<4; ++i){
if(guess[i] == solution[i])
++res.hits;
++guess_count[(int)(guess[i]-'A')];
++solution_count[(int)(solution[i]-'A')];
}
char color[] = "RGBY";
for(int i=0; i<4; ++i){
int idx = (int)(color[i] - 'A');
res.pseudo_hits += Min(guess_count[idx], solution_count[idx]);
}
res.pseudo_hits -= res.hits;
return res;
}
int main(){
char solution[] = "RYGB";
char guess[] = "YRRR";
Result res = Estimate(solution, guess);
cout<<res.hits<<" "<<res.pseudo_hits<<endl;
Result res1 = Estimate1(solution, guess);
cout<<res1.hits<<" "<<res1.pseudo_hits<<endl;
return 0;
}