-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBull_cow.h
57 lines (49 loc) · 1.13 KB
/
Bull_cow.h
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
//
// Bull_cow.h
// algorithm
//
// Created by DanMiao on 12/22/15.
// Copyright © 2015 DanMiao. All rights reserved.
//
#ifndef Bull_cow_h
#define Bull_cow_h
#include <unordered_map>
using namespace std;
string getHint(string secret, string guess) {
int bull_num = 0;
int cow_num = 0;
unordered_map<int, int> record;
vector<bool> flag;
for(unsigned int i = 0; i < secret.size(); i++)
{
if(secret[i] == guess[i])
{
bull_num++;
flag.push_back(true);
}
else
flag.push_back(false);
}
for(unsigned int i = 0; i < secret.size(); i++)
{
if(flag[i] == false)
{
record[secret[i]]++;
}
}
for(unsigned int i = 0; i < guess.size(); i++)
{
if(record.find(guess[i]) != record.end() && record[guess[i]] > 0)
{
record[guess[i]]--;
cow_num++;
}
}
string result;
result.push_back('0' + bull_num);
result.push_back('A');
result.push_back('0' + cow_num);
result.push_back('B');
return result;
}
#endif /* Bull_cow_h */