SRM551

1.ColorfulBricks

#include<iostream>
#include<string>
using namespace std;

class ColorfulBricks {
public:
	int countLayouts(string bricks) {
		int a[26],cnt=0;
		memset(a,0,sizeof(a));
		int sz = bricks.size();
		for(int i=0;i<sz;i++) {
			a[bricks[i]-'A']++;
		}
		for(int j=0;j<26;j++) {
			if(a[j]>0)
				cnt++;
		}
		if(cnt==1)
			return 1;
		if(cnt==2)
			return 2;
		return 0;
	}
};
import java.util.*;

public class ColorfulBricks {
    public int countLayouts(String bricks){
        // create a set S with just one copy of each character in bricks  
        HashSet<Character> S = new HashSet<Character>();
        for(char c : bricks.toCharArray()) 
            S.add(c);

        // S.size() will be the number of different characters in bricks
        if(S.size() == 1) return 1;
        if(S.size() == 2) return 2;
        return 0;
    }
}

 

2.ColorfulChocolates

#include<iostream>
#include<string>
#include<cstdlib>
#define max _MAX
using namespace std;

class ColorfulChocolates {
public:
	int maximumSpread(string C, int maxSwaps) {
		int best = 0;
		int m = C.size();
		for(int i = 0; i < m; i++)
			for(int j = 0; j < m; j++) {
				int cost = 0, spread = 0;
				int k = i, l = j;
				while(k < m && l < m) {
					if(cost + abs(k - l) > maxSwaps) {
						break;
					}
					spread++;
					cost += abs(k - l);
					k += 1;
					while(k < m && C[k] != C[i]) {
						k++;
					}
					l++;
				}
				best = max(best, spread);
			}
			return best;
	}
};

另一种解法:http://www.strongczq.com/2012/08/topcoder-srm551.html

 

3.ColorfulCupcakesDivTwo

#include<iostream>
#include<string>
#define MOD 1000000007
using namespace std;
typedef long  ll;

class ColorfulCupcakesDivTwo {
public:
	int countArrangements(string cupcakes) {
		ll dp[3][3][50][50][50];
		int c0=0,c1=0,c2=0;
		int m=cupcakes.size();
		for(int i=0;i<m;i++){
			if(cupcakes[i]=='A')
				c0++;
			else if(cupcakes[i]=='B')
				c1++;
			else if(cupcakes[i]=='c')
				c2++;
		}
		memset(dp,0,sizeof(dp));
		if(c0>0)
			dp[0][0][1][0][0]=1;
		if(c1>0)
			dp[1][1][0][1][0]=1;
		if(c2>0)
			dp[2][2][0][0][1]=1;
		for(int x0=0;x0<c0+1;x0++)
			for(int x1=0;x1<c1+1;x1++)
				for(int x2=0;x2<c2+1;x2++)
					for(int s=0;s<3;s++)
						for(int e=0;e<3;e++){
							if(dp[s][e][x0][x1][x2]==0){
								continue;
							}
							if(e!=0&&x0<c0){
								dp[s][0][x0+1][x1][x2]+=dp[s][e][x0][x1][x2];
								dp[s][0][x0+1][x1][x2]%=MOD;
							}
							if(e!=1&&x1<c1){
								dp[s][1][x0][x1+1][x2]+=dp[s][e][x0][x1][x2];
								dp[s][1][x0][x1+1][x2]%=MOD;
							}
							if(e!=2&&x2<c2){
								dp[s][2][x0][x1][x2+1]+=dp[s][e][x0][x1][x2];
								dp[s][2][x0][x1][x2+1]%=MOD;
							}
						}
		ll res=0;
		for(int s=0;s<3;s++)
			for(int e=0;e<3;e++){
				if(s!=e)
					res+=dp[s][e][c0][c1][c2];
			}
	return (int)(res%MOD);
	}

};
int dp[3][3][51][51][51];
int cnt[3],n;
class ColorfulCupcakesDivTwo{
public:	
	int slove(int pos,int pre,int fst,int a,int b,int c){
		if(a<0||b<0||c<0)
			return 0;
		if(pos==n)
			return pre!=fst;
		if(dp[fst][pre][a][b][c]!=-1)
			return dp[fst][pre][a][b][c];
		int ret=0;
		if(pos==0){
			ret=(ret+slove(pos+1,0,0,a-1,b,c))%MOD;
			ret=(ret+slove(pos+1,1,1,a,b-1,c))%MOD;
			ret=(ret+slove(pos+1,2,2,a,b,c-1))%MOD;
		}
		else if(pre==0){
			ret=(ret+slove(pos+1,fst,1,a,b-1,c))%MOD;
			ret=(ret+slove(pos+1,fst,2,a,b,c-1))%MOD;
		}
		else if(pre==1){
			ret=(ret+slove(pos+1,fst,0,a-1,b,c))%MOD;
			ret=(ret+slove(pos+1,fst,2,a,b,c-1))%MOD;
		}
		else if(pre==2){
			ret=(ret+slove(pos+1,fst,0,a-1,b,c))%MOD;
			ret=(ret+slove(pos+1,fst,1,a,b-1,c))%MOD;
		}
		return dp[fst][pre][a][b][c]=ret;
	}
	int countArrangements(string cupcakes){	
		cnt[0]=cnt[1]=cnt[2]=0;
		n=cupcakes.size();
		for(int i=0;i<n;i++)
			cnt[cupcakes[i]-'A']++;	
		memset(dp,-1,sizeof(dp));
		return slove(0,0,0,cnt[0],cnt[1],cnt[2]);
	}
	
};


4.ColorfulWolves

#include<iostream>
#include<string>
#include<vector>
#define INF 10000;
using namespace std;

class ColorfulWolves {
public:
	int getmin(vector<string> colormap) {
		int sz = colormap.size();
	//	int col = colormap[0].size();
		int dist[55][55];
		for(int r = 0; r < sz; r++){
			int d=0;
			for(int c = 0; c < sz; c++) {
				if(colormap[r][c] == 'Y')
					dist[r][c] = d++;
				else
					dist[r][c] = 10000;
			}
		}
			for(int k = 0; k < sz; k++)
				for(int i = 0; i < sz; i++)
					for(int j = 0; j < sz; j++) {
						if(dist[i][k] + dist[k][j] < dist[i][j])
							dist[i][j] = dist[i][k] + dist[k][j];
					}
		return dist[0][sz-1]>=10000?-1:dist[0][sz-1];
	}
};




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值