299. Bulls and cows

Source: Internet
Author: User

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:1Bull and3cows. (The Bull is8, the cows is01 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 1st1in friend's guess is a bull, the 2nd or 3rd1is 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.