SRM555

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 function
		int res = 0;
		//use tmp as a draft
		vector <string> tmp;
		//the number of rows and columns of the grid
		int 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 row
				for (int k = 0; k < c; k++)
					tmp[i][k] = (tmp[i][k] == '1' ? '0': '1');

				//and then flipping column jth
				for (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 result
				res = 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];
     }
 }



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值