Microsoft Online Test-2015-4-3 Magic Box && Professor Q ' s software

Source: Internet
Author: User

write in front:

http://blog.csdn.net/michael_kong_nju/article/details/44872519

On the No. 4.3 Microsoft Online Challenge, I feel like brush brushes less, performance in several aspects: 1. Lack of programming experience. 2. The use of the algorithm is not flexible. So the following is to strengthen the training of OJ,

Do more work on the Leetcode. After the 4 questions carefully written debugging, hope and my similar classmates can also step up the code training.

1. The original title of the first question is:

the circus clown Sunny has a magic box. When the circus are performing, Sunny puts some balls into the box one by one. The balls is in three colors:red (R), yellow (Y) and Blue (B). Let Cr, Cy, Cb denote the numbers of red, yellow, blue balls in the box. Whenever the differences among Cr, Cy, Cb happen to is X, Y, Z, all balls in the box vanish. Given x, Y, z and the sequence in which Sunny put the balls, you be to find what's the maximum number of balls in the Bo x  ever

For example, let's assume X=1, y=2, z=3 and the sequence is Rrybrbrybry. After Sunny puts the first 7 balls, RRYBRBR, into the box, Cr, Cy, Cb is 4, 1, 2 respectively. The differences is exactly 1, 2, 3. (| Cr-cy|=3, | Cy-cb|=1, | cb-cr|=2) then all the 7 balls vanish. Finally There is 4 balls in the box, after Sunny puts the remaining balls. So the box contains 7 balls for most, after Sunny puts the first 7 balls and before they vanish.

Input

Line 1:x y Z

Line 2:the sequence consisting of only three characters ' R ', ' Y ' and ' B '.

For 30% data, the length of the sequence are no more than 200.

For 100% data, the length of the sequence are no more than 20,000, 0 <= x, y, Z <= 20.

Output

The maximum number of balls in the box ever.

Sample input:

1 2 3RRYBRBRYBRY

Sample output:

7
In fact, this problem is relatively simple, but the submission of the time there are always some test cases, including myself at the beginning of the time is also, and finally because really can not find out also did not go on. Think back to think about the following points need to be noted:

1. Because the ball in a satisfied condition will vanish, and the condition here is the selective exact match, that is, |cr-cy| can be equal to any one of the three, and the other two of the difference belongs to the other two.

2. Vanish not only happens once when all the balls are put in, so it is a loop to judge.

3. It is possible that the last remaining one is the biggest, so you need to put all the balls out and compare the remaining balls to the vanish of the previous ones.

If you can consider all the above conditions should be no problem, the following is the specific implementation:

#include <iostream>//by lingtao Kong 2015/0404#include <string> #include <cmath>void sort_int (in T &r1, int &r2, int &r3)//in advance, the X, Y, z and difference values can be judged at once without discussion. {if (R1 > R2) swap (R1, R2), if (R1 > R3) Swap (R1, R3), if (R2 > R3) swap (R2, R3);} int Print_max (int x, int y, int z, String box) {sort_int (x, y, z);//To set X < y < Zint red=0, blue=0, Yello=0;int R1, r2, R3;int max = 0;int first = -1;for (int i = 0; i < box.size (); i++) {switch (Box[i]) {case ' R ': red++; Break;case ' Y ': yello++; Break;case ' B ': blue++; Break;default:break;}        R1 = ABS (Red-blue); R2 = ABS (Red-yello), R3 = ABS (Blue-yello); Sort_int (R1, R2, R3); if (r1 = = x && r2 = = y && R3 = z) {max = (I-first > Max)?          (I-first): Max; The use of the difference in the value to calculate the number, some students use three ball color is also very good. First = i;red = 0;blue = 0;yello = 0;}} Return (Max > Box.size () -1-first)?   (max): (Box.size () -1-first); Compare with the rest}void main (void) {int x, y, z;cin >> x &GT;> y >> z;string ball;cin >> ball;cout << print_max (x, Y, Z, ball) << Endl;} 
2. Second question

The problem of the second question is more complicated, if you want to see English I put him down below:

Describe

Professor Q develops a new software. The software consists of n modules which is numbered from 1 to N. The i-th module would be started up by signal SI. If Signal SIis generated multiple times, the I-th module would also be started multiple times. The modules different is started up by the same signal. During its lifecircle, the i-th module would generate KISignals:e1E2,..., EKi. These signals may, start up, modules and so on. Fortunately the software is so carefully designed thatthere is no loop in the starting chain of modules, which means eventually all the modules would be stoped. Professor Q generates some initial signals and want to know what many times each module is started.

Input

The first line contains an integer T, the number of test cases. T test cases follows.

For each test case, the first line contains contains, numbers N and M, indicating the number of modules and number of S Ignals that Professor Q generates initially.

The second line contains M integers, indicating the signals that Professor Q generates initially.

Line 3~n + 2, each line describes an module, following the format S, K, e1, E2, ..., eK. S represents the signal that is the start up this module. K represents the total amount of signals that is generated during the lifecircle of this module. and E1 ... EK is these signals.

For 20% data, all N, M <= 10
For 40% data, all N, M <=3
For 100% data, all 1 <= T <= 5, N, M <=5, 0 <= K <= 3, 0 <= S, E <=5.

Hint:huge input in this problem. Fast IO such as scanf and BufferedReader are recommended.

Output

For each test case, the output a line with n numbers ans1, ans2, ..., ansN. AnsI is the number of times that the I-th module is started. In case the answers is too large, output the answers modulo 142857 (the remainder of division by 142857).

Sample input
33 2123 256123 2 456 256456 3 666 111 256256 1 903 1100100 2 200 200200 1 300200 05 111 2 2 32 2 3 43 2 4 54 2 5 65 2 6 7
Sample output
1 1 31 2 21 1 2 3 5
In fact, the difficulty of this problem I think there are several aspects:

1. The requirements for input control are high, we need to input all variables from the console according to specifications, and also to define the corresponding size variables.

2. It is time-consuming to understand the variables of the topic, because each entry must be understood first.

If the meaning of the topic is clear, then after the effective input, the design algorithm is very simple, only need a queue, and then out of the queue to see which module is activated and the data will be converted

The team will be ready again. The following is the implementation of the code, mainly using dynamic arrays and the queue data structure.

/*by Lingtao Kong 2015/04/04*/#include <iostream> #include <queue>using namespace Std;int N;  Number of test cases output for the first line # define Max_n 100010#define max_m 100010#define K 5//Each row has a maximum of 5 columns, of which 3 output int (*modules_) [max_n][k]; A dynamic three-dimensional array is defined to hold the transformation model corresponding to all test cases.   int *modules_num, *signals_num;  Dynamic arrays are used to hold the number of modules corresponding to each test case and the number of signals int (*signals) [3]; Save all input input signals/* Read data from terminal */void Read_data () {memset (Modules_,-1, sizeof (Modules_)); for (int n = 0; n < n; n++) {cin &GT;&G T   Modules_num[n] >> Signals_num[n]; Enter n mfor for nth test case (int i = 0; i < signals_num[n]; i++) CIN >> signals[n][i];//Enter the corresponding conversion rule for the M module of the nth test case for (int m = 0; M < modules_num[n];   m++) {cin >> modules_[n][m][0] >> modules_[n][m][1]; for (int e = 0; e < modules_[n][m][1]; e++) cin >> modules_[n][m][2 + e];}}} /* processing output, for each test case, the signal in signals corresponds to the module just established */queue<int>qu_sig;void process () {for (int n = 0; n < n; n++) {//    Load the initial signal into the queue. for (int s = 0; s < signals_num[n]; s++) {Qu_sig.push (Signals[n][s]);} int *result = new Int[modules_num[n]];memset (result, 0, sizeof (result) *modules_num[n]); int Element;while (!qu_ Sig.empty ()) {element = Qu_sig.front (); for (int m = 0; m < modules_num[n]; m++) {if (element = = Modules_[n][m][0]) {result [M]++;for (int e = 0; e < modules_[n][m][1]; e++) Qu_sig.push (modules_[n][m][2 + e]);}} Qu_sig.pop ();} for (int i = 0; i < modules_num[n]; i++) cout << result[i] << ""; cout << Endl;delete[]result;}} void Main (void) {cin >> n;modules_num = new int [N];signals_num = new Int[n];signals = new Int[n][3];modules_ = new I Nt[n][max_n][k];read_data ();p rocess ();d Elete[]modules_num;delete[]signals_num;delete[]signals;delete[]modules_ ;}

Confined to the next article will give a third, four questions if you find something wrong or better algorithms welcome criticism. Thank you.

Microsoft Online Test-2015-4-3 Magic Box && Professor Q ' s software

Related Article

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.