SRM 615

dp[5000][50][50]表示到第i个字符,red的个数除以M的余数是j,red比green多k个。


TC题目的难度的把握变弱了很多啊0 0


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

class AlternativePiles {
public:
	int count(string, int);
};
long long pmod = 1000000007;
long long dp[5005][55][55];
int AlternativePiles::count(string s, int m) {
	int i, j, k;
	int n = s.size();
	memset(dp, 0, sizeof(dp));
	dp[0][0][0] = 1;
	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			for (k = 0; k <= m; ++k) {
				if (s[i] == 'R') {
					dp[i + 1][(j + 1) % m][k + 1] += dp[i][j][k];
					dp[i + 1][(j + 1) % m][k + 1] %= pmod;
				} else if (s[i] == 'G') {
					if (k > 0) {
						dp[i + 1][j][k - 1] += dp[i][j][k];
						dp[i + 1][j][k - 1] %= pmod;
					}
				} else if (s[i] == 'B') {
					dp[i + 1][j][k] += dp[i][j][k];
					dp[i + 1][j][k] %= pmod;
				} else {
					dp[i + 1][(j + 1) % m][k + 1] += dp[i][j][k];
					dp[i + 1][(j + 1) % m][k + 1] %= pmod;
					if (k > 0) {
						dp[i + 1][j][k - 1] += dp[i][j][k];
						dp[i + 1][j][k - 1] %= pmod;
					}
					dp[i + 1][j][k] += dp[i][j][k];
					dp[i + 1][j][k] %= pmod;
				}
			}
		}
	}
	return dp[n][0][0];
}


你可能感兴趣的:(topcoder,SRM)