It takes a long time to understand the problem description. The question itself is very simple.
The designer provides a string of code with a length of N, and breaker uses guess to decrypt the code.
For two strings of numbers, if the same column has equal numbers, it is called strong match,
Two equal numbers in different columns are called weak match.
The question requirement is to first output the number of strong and then the number of weak.
By the way, note that
1. Each code can only match once, whether it is strong or match.
2. When outputting (*, *), pay attention to the four leading spaces, because I still pe it once.
If you still do not understand, please refer to here
Let's talk about my algorithm: I opened two more arrays num1 and num2 to put 1 ~ in code and guess respectively ~ 9 the number of times each number appears. First, match strong. After the match, subtract 1 from the corresponding elements of num1 and num2, indicating that the match has been completed. After strong matches, weak is left. Because strong does not exist, weak is directly + = min (Code, guess ).
The following are the original questions:
Mastermind is a game for two players. One of them,Designer, Selects a secret code. The other,Breaker, Tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the lengthNThat a code must have and upon the colors that may occur in a code.
In order to break the code, breaker makes a number of guesses, each guess itself being a code. After each guess designer gives a hint, stating to what extent the guess matches his secret code.
In this problem you will be given a secret code and a guess, and are to determine the hint. A hint consists of a pair of numbers determined as follows.
AMatchIs a pair (I,J), And, such that. Match (I,J) Is calledStrongWhenI=J, And is calledWeakOtherwise. Two matches (I,J) And (P,Q) Are calledIndependentWhenI=PIf and only ifJ=Q. A set of matches is calledIndependentWhen all of its members are pairwise independent.
Designer chooses an Independent SetMOf matches for which the total number of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches inM. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (N, 0), then the guess is identical to the secret code.
Input
The input will consist of data for a number of games. The input for each game begins with an integer specifyingN(The length of the Code). Following these will be the secret code, representedNIntegers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also representedNIntegers, each in the range 1 to 9. Following the Last guess in each game will beNZeroes; these zeroes are not to be considered as a guess.
Following the data for the first game will appear data for the second game (if any) beginning with a new valueN. The last game in the input will be followed by a single zero (when a valueNWocould normally be specified). The maximum valueNWill be 1000.
Output
The output for each game shocould list the hints that wocould be generated for each guess, in order, one hint per line. each hint shocould be represented as a pair of integers enclosed in parentheses and separated by a comma. the entire list of hints for each game shoshould be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. look at the samples below forExactFormat.
Sample Input
41 3 5 51 1 2 34 3 3 56 5 5 16 1 3 51 3 5 50 0 0 0101 2 2 2 4 5 6 6 6 91 2 3 4 5 6 7 8 9 11 1 2 2 3 3 4 4 5 51 2 1 3 1 5 1 6 1 91 2 2 5 5 5 6 6 6 70 0 0 0 0 0 0 0 0 00
Sample output
Game 1: (1,1) (2,0) (1,2) (1,2) (4,0)Game 2: (2,4) (3,2) (5,0) (7,0)
AC code:
1 // # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <algorithm> 6 using namespace STD; 7 8 const int maxn = 1000 + 5; 9 int code [maxn], guess [maxn]; 10 int num1 [10], num2 [10], num3 [10]; // stores the number of occurrences of each number in code and guess 11 // num3 to copy num1 12 13 int main (void) 14 {15 # ifdef local16 freopen ("340in.txt ", "r", stdin); 17 # endif18 19 int N, Kase = 0; 20 while (scanf ("% d", & n) = 1 & N) 21 {22 memset (Code, 0, sizeof (CODE); 23 memset (num1, 0, sizeof (num1); 24 int I; 25 for (I = 0; I <n; ++ I) 26 {27 scanf ("% d", & code [I]); 28 + num1 [Code [I]; 29} 30 printf ("game % d: \ n", ++ Kase); 31 While (true) 32 {33 memset (guess, 0, sizeof (guess )); 34 memset (num2, 0, sizeof (num2); 35 memcpy (num3, num1, sizeof (num1); 36 for (I = 0; I <N; ++ I) 37 {38 scanf ("% d", & guess [I]); 39 ++ num2 [guess [I]; 40} 41 if (guess [0] = 0) 42 break; 43 // count strong 44 int strong = 0; 45 for (I = 0; I <N; ++ I) 46 {47 If (code [I] = guess [I]) 48 {49 + + strong; 50 -- num3 [Code [I]; // each code [I] can only match 51 at a time -- num2 [guess [I]; 52} 53} 54 // calculate weak55 int weak = 0; 56 for (I = 1; I <= 9; ++ I) 57 weak + = min (num3 [I], num2 [I]); 58 printf ("(% d, % d) \ n", strong, weak); 59} 60} 61 Return 0; 62}
Code Jun