第一次做出来第二道题,真不容易啊
第一道题比较水,穷举各种情况就可以了
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 <set> 11 #include <queue> 12 using namespace std; 13 14 class XorBoardDivTwo{ 15 public: 16 int theMax(vector <string> board){ 17 int row=board.size(); 18 int col=board[0].size(); 19 int i,j,k,l,i1,i2; 20 int res=0; 21 vector <string> orign=board; 22 vector <string> tmp,tmp2; 23 for(i=0;i<row;i++){ 24 tmp=orign; 25 for(k=0;k<col;k++){ 26 if(tmp[i][k]=='1') 27 tmp[i][k]='0'; 28 else 29 tmp[i][k]='1'; 30 } 31 for(j=0;j<col;j++){ 32 tmp2=tmp; 33 for (l = 0; l < row; l++) { 34 if (tmp2[l][j] == '1') 35 tmp2[l][j] = '0'; 36 else 37 tmp2[l][j] = '1'; 38 } 39 int res_tmp=0; 40 for(i1=0;i1<row;i1++){ 41 for(i2=0;i2<col;i2++) 42 if(tmp2[i1][i2]=='1') 43 res_tmp++; 44 } 45 res=max(res,res_tmp); 46 47 48 } 49 } 50 return res; 51 } 52 53 };
第二道题就单源最短路径问题
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 <set> 11 #include <queue> 12 #include <stack> 13 using namespace std; 14 15 class CuttingBitString { 16 public: 17 int valid(string var) {//判断是不是1,5,25..... 18 if (var.size() == 1) { 19 if (var[0] == '1') 20 return 1; 21 else 22 return 0; 23 } 24 int var_size = var.size(); 25 if (var[0] == '0') 26 return 0; 27 long long tmp = 0; 28 long long mul = 1; 29 for (int i = var_size - 1; i >= 0; i--) { 30 tmp = (tmp + (var[i] - '0') * mul); 31 mul *= 2; 32 } 33 while (tmp % 5 == 0) { 34 tmp = tmp / 5; 35 } 36 if (tmp == 1) 37 return 1; 38 return 0; 39 } 40 int getmin(string S) { 41 map<int, map<int, int> > path; 42 int i, j; 43 int var_size = S.size(); 44 string tmp; 45 for (i = 0; i < var_size; i++) { 46 for (j = 0; j < var_size; j++) { 47 path[i][j] =0; 48 } 49 } 50 for (i = 0; i < var_size; i++) { 51 for (j = i; j < var_size; j++) { 52 tmp = S.substr(i, j - i + 1); 53 path[i][j] = valid(tmp); 54 } 55 } 56 57 map<int, int> min_path; 58 for (i = 0; i < var_size; i++) { 59 min_path[i] = 1000; 60 } 61 min_path[var_size] = 0; 62 for (i = var_size - 1; i >= 0; i--) { 63 for (j = i; j < var_size - 1; j++) { 64 if (path[i][j] == 1 && min_path[j + 1] < 1000) { 65 min_path[i] = min(min_path[i], 1 + min_path[j+1]); 66 } 67 } 68 if (path[i][var_size - 1]) 69 min_path[i] = min(min_path[i], 1); 70 } 71 72 if (min_path[0] == 0||min_path[0]==1000) 73 return -1; 74 return min_path[0]; 75 76 } 77 };