SRM555

来源:互联网 发布:淘宝如何选择发货地 编辑:程序博客网 时间:2024/06/14 05:40

1.XorBoardDivTwo

#include<iostream>#include<vector>using namespace std;class XorBoardDivTwo{public:int theMax(vector<string> board){int r=board.size();int res=0,temp=0,best=0;int rsum[50]={0},csum[50]={0};int c=board[0].size();for(int i=0;i<r;i++){for(int j=0;j<c;j++){int tem=(board[i][j]=='0')?0:1;rsum[i]+=tem;csum[j]+=tem;temp+=tem;}}for(int i1=0;i1<r;i1++)for(int j=0;j<c;j++){int tem=(board[i1][j]=='0')?0:1;res=temp+r-2*rsum[i1]+c-2*csum[j]+4*tem-2;if(res>best)best=res;}return best;}};
#include <vector>#include <iostream>using namespace std;class XorBoardDivTwo {public:int theMax(vector <string> board) {//result of the functionint res = 0;//use tmp as a draftvector <string> tmp;//the number of rows and columns of the gridint r, c;r = board.size();c = board[0].size();for (int i = 0; i < r; i++)for (int j = 0; j < c; j++) {tmp = board;//try flipping the ith rowfor (int k = 0; k < c; k++)tmp[i][k] = (tmp[i][k] == '1' ? '0': '1');//and then flipping column jthfor (int k = 0; k < r; k++)tmp[k][j] = (tmp[k][j] == '1' ? '0': '1');//counting the number of '1'int cnt = 0;for (int x = 0; x < r; x ++)for (int y = 0; y < c; y++)cnt += tmp[x][y] == '1';//update the resultres = max(res, cnt);}return res;}};


2.CuttingBitString

#include<iostream>#include<string>using namespace std;class CuttingBitString{public:bool check(string s){if(s[0]=='0') return false;//long long t=0;//long long is 64bite, VC is not stand.long t=0;int size=s.size();for(int i=0;i<size;i++){t=t*2+(s[i]=='1');}while(t%5==0)t/=5;return t==1;}int getmin(string s){int F[50];int n=s.size();for(int i=0;i<n;i++){F[i]=-1;if(check(s.substr(0,i+1))){F[i]=1;}else{for(int j=1;j<=i;j++){if(check(s.substr(j,i-j+1))&&F[j-1]!=-1){if(F[i]>F[j-1]+1||F[i]==-1){F[i]=F[j-1]+1;}}}}}return F[n-1];}};
public class CuttingBitString {     public int getmin(String S)     {         Set<Long> powof5 = new HashSet<Long>();         for(long v = 1; v < (1L << 55); v *= 5){             powof5.add(v);         }         char[] s = S.toCharArray();         int[] dp = new int[s.length + 1];         Arrays.fill(dp, Integer.MAX_VALUE);         dp[s.length] = 0;         for(int i = s.length - 1; i >= 0; --i){             if(s[i] == '0'){                 continue;             }             long val = 0;             for(int j = i + 1; j <= s.length; ++j){                 val = (val << 1) + (s[j - 1] - '0');                 if(powof5.contains(val)){                     if(dp[j] != Integer.MAX_VALUE){                         dp[i] = Math.min(dp[i], dp[j] + 1);                     }                 }             }         }         return dp[0] == Integer.MAX_VALUE ? -1 : dp[0];     } }



 

原创粉丝点击