您的位置:首页 > 其它

SRM 555 DIV2

2012-09-08 23:43 447 查看
第一次做出来第二道题,真不容易啊

第一道题比较水,穷举各种情况就可以了

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <map>
#include <algorithm>
#include <list>
#include <ctime>
#include <set>
#include <queue>
using namespace std;

class XorBoardDivTwo{
public:
int theMax(vector <string> board){
int row=board.size();
int col=board[0].size();
int i,j,k,l,i1,i2;
int res=0;
vector <string> orign=board;
vector <string> tmp,tmp2;
for(i=0;i<row;i++){
tmp=orign;
for(k=0;k<col;k++){
if(tmp[i][k]=='1')
tmp[i][k]='0';
else
tmp[i][k]='1';
}
for(j=0;j<col;j++){
tmp2=tmp;
for (l = 0; l < row; l++) {
if (tmp2[l][j] == '1')
tmp2[l][j] = '0';
else
tmp2[l][j] = '1';
}
int res_tmp=0;
for(i1=0;i1<row;i1++){
for(i2=0;i2<col;i2++)
if(tmp2[i1][i2]=='1')
res_tmp++;
}
res=max(res,res_tmp);

}
}
return res;
}

};


第二道题就单源最短路径问题

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <map>
#include <algorithm>
#include <list>
#include <ctime>
#include <set>
#include <queue>
#include <stack>
using namespace std;

class CuttingBitString {
public:
int valid(string var) {//判断是不是1,5,25.....
if (var.size() == 1) {
if (var[0] == '1')
return 1;
else
return 0;
}
int var_size = var.size();
if (var[0] == '0')
return 0;
long long tmp = 0;
long long mul = 1;
for (int i = var_size - 1; i >= 0; i--) {
tmp = (tmp + (var[i] - '0') * mul);
mul *= 2;
}
while (tmp % 5 == 0) {
tmp = tmp / 5;
}
if (tmp == 1)
return 1;
return 0;
}
int getmin(string S) {
map<int, map<int, int> > path;
int i, j;
int var_size = S.size();
string tmp;
for (i = 0; i < var_size; i++) {
for (j = 0; j < var_size; j++) {
path[i][j] =0;
}
}
for (i = 0; i < var_size; i++) {
for (j = i; j < var_size; j++) {
tmp = S.substr(i, j - i + 1);
path[i][j] = valid(tmp);
}
}

map<int, int> min_path;
for (i = 0; i < var_size; i++) {
min_path[i] = 1000;
}
min_path[var_size] = 0;
for (i = var_size - 1; i >= 0; i--) {
for (j = i; j < var_size - 1; j++) {
if (path[i][j] == 1 && min_path[j + 1] < 1000) {
min_path[i] = min(min_path[i], 1 + min_path[j+1]);
}
}
if (path[i][var_size - 1])
min_path[i] = min(min_path[i], 1);
}

if (min_path[0] == 0||min_path[0]==1000)
return -1;
return min_path[0];

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: