Logo Wy Online Judge

WyOJ

Paste!

#include <array>
#include <iostream>
#include <string>
#include <vector> 

constexpr int Mod = 1000000007;

int main() {
	using namespace std;
	int n, q;
	cin >> n >> q;
	string cloth;
	cin >> cloth;
	
	array<vector<vector<vector<int>>>, 3> dp;
	dp.fill(vector(n + 1, vector(n + 1, vector<int>(n + 1, 0))));
	vector sum(n + 1, vector(n + 1, vector<int64_t>(n + 1, 0)));
	switch(cloth.front())
	{
		case 'a':
			dp[0][1][0][0] = 1;
		break;
		case 'b':
			dp[1][0][1][0] = 1;
		break;
		case 'c':
			dp[2][0][0][1] = 1;
		break;
		default:
			dp[0][1][0][0] = dp[1][0][1][0]
				= dp[2][0][0][1] = 1;
	}
	for(int tot = 2; tot <= n; tot++)
	{
		int fixa = cloth[tot - 1] == 'a',
			fixb = cloth[tot - 1] == 'b',
			fixc = cloth[tot - 1] == 'c',
			nofix = cloth[tot - 1] == '?';
		for(int a = fixa; a <= tot; a++)
			for(int b = fixb; a + b + fixc <= tot; b++)
			{
				int c = tot - a - b;
				if(fixa || nofix && a)
					dp[0][a][b][c] = (dp[1][a - 1][b][c] + dp[2][a - 1][b][c]) % Mod;
				if(fixb || nofix && b)
					dp[1][a][b][c] = (dp[0][a][b - 1][c] + dp[2][a][b - 1][c]) % Mod;
				if(fixc || nofix && c)
					dp[2][a][b][c] = (dp[0][a][b][c - 1] + dp[1][a][b][c - 1]) % Mod;
				if(tot == n)
					sum[a][b][c] = dp[0][a][b][c] + dp[1][a][b][c] + dp[2][a][b][c];
			}
	}
	
	int na = count(cloth.begin(), cloth.end(), 'a'),
		nb = count(cloth.begin(), cloth.end(), 'b'),
		nc = count(cloth.begin(), cloth.end(), 'c');
	if(n == 1)
	{
		sum[1][0][0] = dp[0][1][0][0];
		sum[0][1][0] = dp[1][0][1][0];
		sum[0][0][1] = dp[2][0][0][1];
	}
	for(int i = 1; i <= n; i++)
		for(int j = 0; j <= n; j++)
			for(int k = 0; k <= n; k++)
				sum[i][j][k] += sum[i - 1][j][k];
	for(int i = 0; i <= n; i++)
		for(int j = 1; j <= n; j++)
			for(int k = 0; k <= n; k++)
				sum[i][j][k] += sum[i][j - 1][k];
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= n; j++)
			for(int k = 1; k <= n; k++)
			{
				sum[i][j][k] += sum[i][j][k - 1];
				sum[i][j][k] %= Mod;
			}
	
	while(q--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		x = min(x + na, n);
		y = min(y + nb, n);
		z = min(z + nc, n);
		cout << sum[x][y][z] << endl;
	}
	return 0;
}