srm 579 DIV1

1、div的水平太难了,加上clean code看多了,写的太长了,凑活着看吧....

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <map>
 7 #include <stack>
 8 #include <algorithm>
 9 #include <list>
10 #include <ctime>
11 #include <set>
12 #include <queue>
13 typedef long long ll;
14 using namespace std;
15 #define CLR(arr, what) memset(arr, what, sizeof(arr))
16 class UndoHistory {
17 public:
18     void addone(map<string, int>& allstate, string& var) {
19         allstate[var] = 1;
20     }
21     bool checkexist(map<string, int>& allstate, string& var) {
22         if (allstate[var] == 1) {
23             return true;
24         }
25         return false;
26     }
27     int checklong(map<string, int>& allstate, string& target){
28         int sz=target.size();
29         int i;
30         bool judge;
31         string cur;
32         int res=0;
33         for(i=0;i<sz;i++){
34             cur=target.substr(0,i+1);
35             judge=checkexist(allstate,cur);
36             if(judge){
37                 res=i+1;
38             }
39         }
40         return res;
41     }
42     bool checkpre(string& pre,string& cur){
43         int szpre=pre.size();
44         int szcur=cur.size();
45         if(szcur<szpre){
46             return false;
47         }
48         for(int i=0;i<szpre;i++){
49             if(pre[i]!=cur[i]){
50                 return false;
51             }
52         }
53         return true;
54     }
55     int minPresses(vector<string> lines) {
56         map<string, int> allstate;
57         string tmp;
58         int sz = (lines[0]).size();
59         for (int i = 0; i < sz; i++) {
60             tmp = (lines[0]).substr(0, i + 1);
61             addone(allstate, tmp);
62         }
63         int res=sz+1;
64         sz=lines.size();
65 
66         for(int i=1;i<sz;i++){
67             int szpre=lines[i-1].size();
68             int szcur=lines[i].size();
69             int his=checklong(allstate,lines[i]);
70             bool judge=checkpre(lines[i-1],lines[i]);
71             int b=szcur-his+2+1;
72             if(judge){
73                 int a=szcur-szpre+1;
74                 res=res+min(a,b);
75             }else{
76                 res=res+b;
77             }
78             for(int j=0;j<szcur;j++){
79                 tmp=(lines[i]).substr(0,j+1);
80                 addone(allstate,tmp);
81             }
82         }
83         return res;
84 
85     }
86 };

2、其实就是状态压缩啊,petr的代码很给力啊~~~

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 #include <list>
 9 #include <ctime>
10 #include <string.h>
11 #include <queue>
12 #include <sstream>
13 using namespace std;
14 const int INF = 99999999;
15 class TravellingPurchasingMan {
16 public:
17     int OneCount(unsigned int x) {
18         int count;
19         for (count = 0; x > 0; count++)
20             x &= x - 1; //把最后面的1变0
21         return count;
22     }
23     int maxStores(int N, vector<string> interestingStores,
24             vector<string> roads) {
25         vector<vector<int> > dist(N, vector<int>(N, INF));
26         for (int i = 0; i < N; ++i) {
27             dist[i][i] = 0;
28         }
29         stringstream ssvar;
30         int sz = interestingStores.size();
31         int sz2 = roads.size();
32         for (int i = 0; i < sz2; i++) {
33             ssvar.clear();
34             ssvar << roads[i];
35             int a, b, c;
36             ssvar >> a >> b >> c;
37             dist[a][b] = min(dist[a][b], c);
38             dist[b][a] = min(dist[b][a], c);
39         }
40         for (int k = 0; k < N; ++k)
41             for (int i = 0; i < N; ++i)
42                 for (int j = 0; j < N; ++j)
43                     dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
44         vector<int> open(sz, 0);
45         vector<int> close(sz, 0);
46         vector<int> duration(sz, 0);
47 
48         for (int i = 0; i < sz; ++i) {
49             ssvar.clear();
50             ssvar << interestingStores[i];
51             int a, b, c;
52             ssvar >> a >> b >> c;
53             open[i] = a;
54             close[i] = b;
55             duration[i] = c;
56         }
57         vector<vector<int> > best(sz, vector<int>(1 << sz, INF));
58 
59         best[0][0] = 0;
60         int res = 0;
61         for (int set = 0; set < (1 << sz); ++set) {
62             int bits = OneCount(set);
63             for (int last = 0; last < sz; ++last) {
64                 int earliest = best[last][set];
65                 if (earliest >= INF)
66                     continue;
67                 res = max(res, bits);
68                 int curPos = last;
69                 if (set == 0)
70                     curPos = N - 1;
71                 for (int dest = 0; dest < sz; ++dest) {
72                     if ((set & (1 << dest)) == 0) {//没有去过
73                         int toGet = earliest + dist[curPos][dest];
74                         if (toGet > close[dest])//到达时间晚了
75                             continue;
76                         if (toGet < open[dest])
77                             toGet = open[dest];
78                         toGet += duration[dest];
79                         if (toGet < best[dest][set ^ (1 << dest)]) {
80                             best[dest][set ^ (1 << dest)] = toGet;
81                         }
82                     }
83                 }
84             }
85         }
86         return res;
87     }
88 };

 

from kakamilan

posted on 2013-05-19 01:58  kakamilan  阅读(249)  评论(0编辑  收藏  举报

导航