|
题目思路很明白了,DFS,实际写起来出了两次错,第一次是将输入的vector<string> 转换成vector<vector<int> >的时候其中的一个while循环里没有考虑到循环到最后那个数字时是‘\0'的时候,这样编译就是出错。第二个问题是在dfs循环里忘记写cur > vector<vector<int> >.size()要return了,这样一漏也导致了编译出错。记住编译出错肯定是程序运行到一个不可能的或者是忘记加终止条件的情况,不会是因为逻辑错误。整个程序写下来没有逻辑上的错误。
1 // BEGIN CUT HERE 2 3 // END CUT HERE 4 #include <functional> 5 #include <algorithm> 6 #include <stdexcept> 7 #include <iostream> 8 #include <sstream> 9 #include <fstream> 10 #include <iomanip> 11 #include <cstdlib> 12 #include <cstring> 13 #include <utility> 14 #include <cctype> 15 #include <vector> 16 #include <string> 17 #include <bitset> 18 #include <queue> 19 #include <stack> 20 #include <ctime> 21 #include <list> 22 #include <map> 23 #include <set> 24 #include <math.h> 25 26 using namespace std; 27 28 #define pb push_back 29 #define INF 100000000000 30 #define L(s) (int)((s).size()) 31 #define FOR(i,a,b) for (int _n(b), i(a); i<=_n; i++) 32 #define rep(i,n) FOR(i,1,(n)) 33 #define rept(i,n) FOR(i,0,(n)-1) 34 #define rept2(i, m, j, n) FOR(i, 0, (m)-1) FOR(j, 0, (n)-1) 35 #define rep2(i, m, j, n) FOR(i, 1, (m)) FOR(j, 1, (n)) 36 #define C(a) memset((a), 0, sizeof(a)) 37 #define ll long long 38 #define VI vector<int> 39 #define ppb pop_back 40 #define mp make_pair 41 #define MOD 1000000007 42 int toInt(string s){ istringstream sin(s); int t; sin>>t;return t;} 43 int stringToInt(string s) { 44 int ret = 0; 45 rept(i, L(s)) { 46 ret = ret*10 + int(s[i]-'0'); 47 } 48 return ret; 49 } 50 bool cmp(const string s1, const string s2) { 51 int cur = 0; 52 while (L(s1) > cur && L(s2) > cur) { 53 if (s1[cur] < s2[cur]) return true; 54 else if (s1[cur] > s2[cur]) return false; 55 else cur++; 56 } 57 if (s1[cur] == '\0') return true; 58 else return false; 59 } 60 61 class PickTeam 62 { 63 public: 64 void dfs(vector<vector<int> > &p, int cur, int &ans, VI &tmp, int dep, int teamSize, int &res, VI &result) { 65 if (dep == teamSize) { 66 if (res > ans) { 67 ans = res; 68 result = tmp; 69 return; 70 } 71 } 72 if (cur >= L(p)) return; 73 dfs(p, cur+1, ans, tmp, dep, teamSize, res, result); 74 int pre = res; 75 for (int j = 0; j < L(tmp); j++) { 76 res += p[cur][tmp[j]]; 77 } 78 tmp.pb(cur); 79 dfs(p, cur+1, ans, tmp, dep+1, teamSize, res, result); 80 res = pre; 81 tmp.ppb(); 82 } 83 vector <string> pickPeople(int teamSize, vector <string> people) 84 { 85 vector<vector<int> > p; 86 vector<string> save; 87 rept(i, L(people)) { 88 VI ps; 89 int cur = 0; 90 string savetmp = ""; 91 while (people[i][cur] != ' ') { 92 savetmp += people[i][cur]; 93 cur++; 94 } 95 save.pb(savetmp); 96 string tmp; 97 while (people[i][cur] != '\0') { 98 tmp = ""; 99 cur++; 100 while (people[i][cur] != ' ') { 101 tmp += people[i][cur]; 102 cur++; 103 if (people[i][cur] == '\0') break; 104 } 105 if (tmp[0] == '-') { 106 tmp.erase(0, 1); 107 int x = stringToInt(tmp); 108 ps.pb(-x); 109 } 110 else ps.pb(stringToInt(tmp)); 111 } 112 p.pb(ps); 113 } 114 int ans = INT_MIN; 115 VI tmp; 116 VI result; 117 int res = 0; 118 dfs(p, 0, ans, tmp, 0, teamSize, res, result); 119 vector<string> ret; 120 rept(i, teamSize) { 121 ret.pb(save[result[i]]); 122 } 123 sort(ret.begin(), ret.end(), cmp); 124 return ret; 125 } 126 127 // BEGIN CUT HERE 128 public: 129 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } 130 private: 131 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 132 void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } 133 void test_case_0() { int Arg0 = 3; string Arr1[] = {"ALICE 0 1 -1 3", 134 "BOB 1 0 2 -4", 135 "CAROL -1 2 0 2", 136 "DAVID 3 -4 2 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "ALICE", "CAROL", "DAVID" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(0, Arg2, pickPeople(Arg0, Arg1)); } 137 void test_case_1() { int Arg0 = 2; string Arr1[] = {"ALICE 0 1 -1 3", 138 "BOB 1 0 2 -4", 139 "CAROL -1 2 0 2", 140 "DAVID 3 -4 2 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "ALICE", "DAVID" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(1, Arg2, pickPeople(Arg0, Arg1)); } 141 void test_case_2() { int Arg0 = 2; string Arr1[] = {"A 0 -2 -2","B -2 0 -1","C -2 -1 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "B", "C" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(2, Arg2, pickPeople(Arg0, Arg1)); } 142 void test_case_3() { int Arg0 = 13; string Arr1[] = {"A 0 2 8 7 1 -4 -3 1 8 2 8 2 1 -3 7 1 3 9 -6 -5", 143 "A 2 0 0 7 -7 -5 8 -8 -9 -9 6 -9 -4 -8 8 1 7 0 3 3", 144 "A 8 0 0 -5 -5 -7 1 -3 1 9 -6 6 1 5 6 -9 8 6 -8 -8", 145 "A 7 7 -5 0 7 -5 3 9 8 3 -6 -5 -5 2 -6 2 -2 -1 -2 8", 146 "B 1 -7 -5 7 0 7 -2 -9 3 7 0 -2 1 1 -9 -3 -2 2 1 -2", 147 "B -4 -5 -7 -5 7 0 4 8 6 0 -1 4 1 -2 -9 4 0 -7 6 -2", 148 "B -3 8 1 3 -2 4 0 8 -1 1 -2 -7 5 0 -6 9 0 -3 1 3", 149 "B 1 -8 -3 9 -9 8 8 0 0 -2 5 0 5 8 3 5 2 4 5 4", 150 "C 8 -9 1 8 3 6 -1 0 0 8 -3 8 -6 -4 7 -4 1 -5 5 4", 151 "C 2 -9 9 3 7 0 1 -2 8 0 -9 -6 6 5 -8 -3 -8 2 2 4", 152 "C 8 6 -6 -6 0 -1 -2 5 -3 -9 0 2 7 8 2 -6 -4 -6 4 4", 153 "C 2 -9 6 -5 -2 4 -7 0 8 -6 2 0 0 -3 6 7 -1 2 -4 -2", 154 "D 1 -4 1 -5 1 1 5 5 -6 6 7 0 0 -7 -4 8 -6 -3 4 -6", 155 "D -3 -8 5 2 1 -2 0 8 -4 5 8 -3 -7 0 7 -3 5 -8 5 1", 156 "D 7 8 6 -6 -9 -9 -6 3 7 -8 2 6 -4 7 0 9 -5 -5 -8 3", 157 "D 1 1 -9 2 -3 4 9 5 -4 -3 -6 7 8 -3 9 0 -2 -7 8 -7", 158 "E 3 7 8 -2 -2 0 0 2 1 -8 -4 -1 -6 5 -5 -2 0 6 0 5", 159 "E 9 0 6 -1 2 -7 -3 4 -5 2 -6 2 -3 -8 -5 -7 6 0 4 8", 160 "E -6 3 -8 -2 1 6 1 5 5 2 4 -4 4 5 -8 8 0 4 0 1", 161 "E -5 3 -8 8 -2 -2 3 4 4 4 4 -2 -6 1 3 -7 5 8 1 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "A", "A", "B", "B", "B", "B", "C", "C", "C", "D", "D", "D", "E" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(3, Arg2, pickPeople(Arg0, Arg1)); } 162 163 // END CUT HERE 164 165 }; 166 // BEGIN CUT HERE 167 int main() 168 { 169 PickTeam ___test; 170 ___test.run_test(-1); 171 return 0; 172 } 173 // END CUT HERE