微軟線上筆試-2015-4-3(1,2題) Magic Box && Professor Q's Software

來源:互聯網
上載者:User

標籤:微軟   magic box   professor qs softwar   

寫在前面:

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

關於4.3號的微軟線上挑戰賽,感覺自己還是刷題刷少了,表現在幾個方面:1. 編程經驗不足。2. 演算法的使用不靈活。所以下面還是要加強OJ的訓練,

把Leetcode上的題多做做。後面又把4道題仔細的編寫調試了一下,希望和我情況類似的同學也能加緊代碼的訓練。

1. 第一題的原題是:

The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are 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 be x, y, z, all balls in the box vanish. Given x, y, z and the sequence in which Sunny put the balls, you are to find what is the maximum number of balls in the box 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 are 4, 1, 2 respectively. The differences are exactly 1, 2, 3. (|Cr-Cy|=3, |Cy-Cb|=1, |Cb-Cr|=2) Then all the 7 balls vanish. Finally there are 4 balls in the box, after Sunny puts the remaining balls. So the box contains 7 balls at most, after Sunny puts the first 7 balls and before they vanish.

輸入

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 is no more than 200.

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

輸出

The maximum number of balls in the box ever.

範例輸入: 

1 2 3RRYBRBRYBRY

範例輸出:

7
其實這道題比較簡單了,但是提交的時候總有些測試案例過不去,包括我自己剛開始的時候也是,最後因為實在找不出來也就沒有繼續下去。後面想了想大概是有下面幾點需要注意的:

1. 因為球在一滿足條件下會vanish,而這裡的條件是選擇性的精確匹配,即|cr-cy|可以等於1,2,3中的任意一個,而其另外兩個的差值屬於其它兩個就可以了。

2. vanish在將所有的球都放進去的時候不止發生一次,所以要迴圈的去判斷。

3. 有可能最後剩下的那個才是最大的,所以需要把所有的球都放完後將剩下的球和之前vanish的最大值做比較。

如果能夠將上面所有的條件都考慮了應該就沒有問題了,下面是具體的實現:

#include <iostream>         //by Lingtao Kong 2015/0404#include <string>#include <cmath>void sort_int(int &r1, int &r2, int &r3)          //提前將x,y,z和差值進行排序就可以一次進行判斷而不需要討論了。{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;          //使用的是下標的差值來計算個數的,有的同學使用的是三個球顏色之和也很好。first = i;red = 0;blue = 0;yello = 0;}}return (max > box.size()-1 - first) ? (max) : (box.size()-1 - first);   //和剩下的進行比較}void  main(void){int x, y, z;cin >> x >> y >> z;string ball;cin >> ball;cout << print_max(x, y, z, ball) << endl;}
2. 第二道題

第二道題的題目比較複雜,如果你想看看英文我把他放在下面了:

描述

Professor Q develops a new software. The software consists of N modules which are numbered from 1 to N. The i-th module will be started up by signal Si. If signal Si is generated multiple times, the i-th module will also be started multiple times. Two different modules may be started up by the same signal. During its lifecircle, the i-th module will generate Ki signals: E1, E2, ..., EKi. These signals may start up other modules and so on. Fortunately the software is so carefully designed that there is no loop in the starting chain of modules, which means eventually all the modules will be stoped. Professor Q generates some initial signals and want to know how many times each module is started.

輸入

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 two numbers N and M, indicating the number of modules and number of signals 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 start up this module. K represents the total amount of signals that are generated during the lifecircle of this module. And E1 ... EK are these signals.

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

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

輸出

For each test case, 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 may be too large, output the answers modulo 142857 (the remainder of division by 142857).

範例輸入
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
範例輸出
1 1 31 2 21 1 2 3 5
其實這道題目的痛點我認為有下面幾個方面:

1. 對輸入控制的要求較高,我們需要從控制台根據規範將所有的變數進行對應輸入,同時還要進行相應大小變數的定義。

2. 對題目的變數的理解比較耗時,因為首先要把每個輸入項搞懂。

如果對題目的意思很明白了,那麼在有效進行輸入之後,設計演算法就很簡單了,只需要一個隊列,然後將出隊列的時候看啟用的是哪個module並且將轉換的資料

再次入隊就可以了。下面是代碼的實現,主要使用了動態數組以及queue資料結構。

/*By Lingtao Kong 2015/04/04*/#include <iostream>#include <queue>using namespace std;int N;  // 第一行輸出的測試案例的數目#define MAX_N 100010#define MAX_M 100010#define K 5  //每一行最多就只有5列,其中3個輸出int  (*modules_)[MAX_N][K];  //定義了一個動態三維數組用來儲存所有測試案例對應的轉換模型。int *modules_num, *signals_num;   //動態數組用來儲存每個測試案例對應的模組個數以及訊號數量int(*signals)[3];  //儲存所有的輸入輸入的訊號/*從終端讀入資料*/void read_data(){memset(modules_, -1, sizeof(modules_));for (int n = 0; n < N; n++){cin >> modules_num[n] >> signals_num[n];   //輸入第n個測試案例的 N Mfor (int i = 0; i < signals_num[n]; i++)cin >> signals[n][i];//輸入第n個測試案例的第m個模組的對應的轉換規則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];}}}/*處理輸出,對於每一個測試案例,將signals中的訊號對應到剛才建立的模組中去*/queue<int>qu_sig;void process(){for (int n = 0; n < N; n++){//將初始訊號裝入隊列。    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 int[N][MAX_N][K];read_data();process();delete[]modules_num;delete[]signals_num;delete[]signals;delete[]modules_;}

限於篇幅在下一篇會給出第三,四題如果你發現有什麼錯誤或者更好的演算法歡迎批評指正。謝謝。

微軟線上筆試-2015-4-3(1,2題) Magic Box && Professor Q's Software

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.