You is playing the following Bulls and cows game with your friend:you write down a number and ask your friend to guess W Hat the number is. Each time your friend makes a guess, you provide a hint the indicates how many digits in said guess match your secret num ber exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the Wron G position (called "cows"). Your Friend would use successive guesses and hints to eventually derive the secret number.
For example:
Secret Number: "1807" Friend's guess: "7810"
Hint:1
Bull and3
cows. (The Bull is8
, the cows is0
, 1
and7
.)
write a function to return a hint according to the secret number and friend's guess, Use a
to indicate the Bulls and b
to indicate the cows. In the above example, your function should Return "1a3b"
.
Please note that both secret number and friend's guess may contain duplicate digits, for example:
Secret number: "1123" Friend's guess: "0111"
in this case, the 1st1
in friend's guess is a bull, the 2nd or 3rd1
is a cow, and your function should return"1A1B"
.
Assume that the secret number and your friend's guess only contain digits, and their lengths is always equal.
Test instructions
String A is compared to string B to ensure that the lengths of a and b are equal.
Bull: The corresponding subscript under the character is equal, bull++
Cow: the string B and string A have the same characters but different subscripts.
Record the number of occurrences of character C with arrays and unordered_map<char,int> table
Method One: Scan two times
First time: Calculate bull, scan a, b
Second time: Calculate cow, scan B
Method Two: Scan once
During the scanning process, assuming the subscript I, the character of a ca,b is CB, during the scanning process, table[ca]++,table[cb]--;
Situation 1:TABLE[CA]==TABLE[CB] then bull++;
Scenario 2:
A) table[ca]<0, the current character CA appears in string B when scanning the subscript of [0--i-1] (table[cb]--, because it performs a minus operation on CB in string b), this case is handled by The character C appears late in the occurrence of string a than in string B.
b) Table[cb]>0, indicates that the current character CB in the scanning [0--i-1] subscript, in string A has occurred (table[ca]++, because of the CA in string A to perform an additional operation), this situation is handled, the character C in the occurrence of string a than string B appears earlier.
For A, b two cases must be judged. After you have judged A, B, perform the corresponding-and + + operations,
string gethint (string secret, string guess) { int len_s=secret.length (), Len_g=guess.length (); if (len_s==0) return ""; int numa=0,numb=0; //unordered_map<char,int > table; int table[10]={0}; /*for (int i=0;i<len_s;++i) { if (Secret[i]==guess[i]) { numa++; } table[secret[i]]++; } for (int i=0;i<len_s;++i) { if ( Table.find (Guess[i])!=table.end () &&table[guess[i]]>0) { --table[guess[i]]; numb++; } }*/ for (int i=0;i<len_s;++i) { if (Secret[i]==guess[i]) { numa++; } else{ if (table[secret[i]-' 0 ']++<0) numb++; if (table[guess[i]-' 0 ']-->0) numb++; / /table[secret[i]]++; //table[guess[i]]--; } } //cout<<numa<<numb-numa; //string res=to_string (NUMA) + "A" +to_string (numb) + "B"; return to_string (NUMA) + "A" +to_string (numb) + "B"; }
Return To_string (NUMA) + "A" +to_string (numb) + "B" reduces the string build and copy process once. You can use Rvo to reduce the time overhead by using arrays over unordered_map<>, from 12ms to 4ms,
299. Bulls and cows