SRM 556 DIV2

 1、我去,这个竟然是前两天百度的面试题,不是最优就挂掉,看来面试官也在网上搜新题啊。

#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

class ChocolateBar {
public:
    int maxLength(string letters) {
        int res = 0;
        vector<int> last(256,-1);
        int l_size = letters.size();
        int cur_min=0;
        for (int i = 0; i < l_size; i++) {
            int cur=letters[i];
            cur_min=min(cur_min+1,i-last[cur]);
            last[cur]=i;
            res=max(cur_min,res);
        }
        return res;
    }
};

 

 

 2、感觉像是在考我们对队列的应用。

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 
 7 class XorTravelingSalesman {
 8 public:
 9     int maxProfit(vector<int> cityValues, vector<string> roads) {
10         int val_size = cityValues.size();
11 
12         vector<int> in(val_size, 0);
13         vector<vector<int> > judge(val_size, vector<int>(1024, 0));
14 
15         queue<int> my_queue;
16         my_queue.push(0);
17         in[0] = 1;
18         judge[0][cityValues[0]] = 1;
19 
20         int res = 0;
21 
22         while (!my_queue.empty()) {
23             int cur = my_queue.front();
24             my_queue.pop();
25             in[cur] = 0;
26             int i = 0;
27             int cur_max = 0;
28             for (i = 0; i < 1024; i++) {
29                 if (judge[cur][i]) {
30                     cur_max = i;
31                     res = max(res, cur_max);
32                     int k;
33                     for (k = 0; k < roads[0].size(); k++) {
34                         if (roads[cur][k] == 'Y'
35                                 && (0 == judge[k][cur_max ^ cityValues[k]])) {
36                             if (0 == in[k]) {
37                                 in[k] = 1;
38                                 my_queue.push(k);
39                             }
40                             judge[k][cityValues[k] ^ cur_max] = 1;
41                         }
42                     }
43                 }
44             }
45             res = max(res, cur_max);
46 
47         }
48         return res;
49     }
50 };

 

3、只要分清两种可能就可以了,最小的数字只能由之前的最小的非0开头的数字和本数字结合,或者之前最小的0开头的数字和本数字结合。

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 
 7 class LeftRightDigitsGame {
 8 public:
 9     string minNumber(string digits) {
10         vector<string> cur;
11         vector<string>::iterator it_cur;
12         int i, j;
13 
14         int size_digits = digits.length();
15         string zero, non_zero;
16         string tmp;
17         tmp += digits[0];
18         cur.push_back(tmp);
19         for (i = 1; i < size_digits; i++) {
20             sort(cur.begin(), cur.end());
21             int zero_exi = 0;
22             int non_zero_exi = 0;
23             for (j = cur.size() - 1; j >= 0; j--) {//倒序保证是最小的
24                 if (cur[j][0] == '0') {
25                     zero = cur[j];
26                     zero_exi = 1;
27                 } else {
28                     non_zero = cur[j];
29                     non_zero_exi = 1;
30                 }
31             }
32             cur.clear();//为下一次循环做准备
33             if (zero_exi) {
34                 cur.push_back(zero + digits[i]);
35                 cur.push_back(digits[i] + zero);
36             }
37             if (non_zero_exi) {
38                 cur.push_back(non_zero + digits[i]);
39                 cur.push_back(digits[i] + zero);
40             }
41         }
42         sort(cur.begin(), cur.end());
43         string res;
44         for (j = cur.size() - 1; j >= 0; j--) {
45             if (cur[j][0] != '0') {//挑出最小的
46                 res = cur[j];
47             }
48         }
49         return res;
50     }
51 };

 

 

 

 

posted on 2012-09-30 16:07  kakamilan  阅读(194)  评论(0编辑  收藏  举报

导航