「代码比对2」CF149D Coloring Brackets

阳间人的代码:

void SFD(int l,int r){
	if(r==l+1)f[l][r][0][1]=f[l][r][0][2]=f[l][r][1][0]=f[l][r][2][0]=1;
	else{
		if(match[l]!=r){
			SFD(l,match[l]);SFD(match[l]+1,r);
			if(match[l]+1!=match[r]){
				f[l][r][0][0]=f[l][match[l]][0][1]*(f[match[l]+1][r][2][0]+f[match[l]+1][r][0][0])+f[l][match[l]][0][2]*(f[match[l]+1][r][1][0]+f[match[l]+1][r][0][0]);
				f[l][r][1][0]=f[l][match[l]][1][0]*(f[match[l]+1][r][0][0]+f[match[l]+1][r][1][0]+f[match[l]+1][r][2][0]);
				f[l][r][2][0]=f[l][match[l]][2][0]*(f[match[l]+1][r][0][0]+f[match[l]+1][r][1][0]+f[match[l]+1][r][2][0]);
				f[l][r][0][1]=f[l][match[l]][0][1]*(f[match[l]+1][r][2][1]+f[match[l]+1][r][0][1])+f[l][match[l]][0][2]*(f[match[l]+1][r][1][1]+f[match[l]+1][r][0][1]);
				f[l][r][1][1]=f[l][match[l]][1][0]*(f[match[l]+1][r][0][1]+f[match[l]+1][r][1][1]+f[match[l]+1][r][2][1]);
				f[l][r][2][1]=f[l][match[l]][2][0]*(f[match[l]+1][r][0][1]+f[match[l]+1][r][1][1]+f[match[l]+1][r][2][1]);
				f[l][r][0][2]=f[l][match[l]][0][1]*(f[match[l]+1][r][0][2]+f[match[l]+1][r][2][2])+f[l][match[l]][0][2]*(f[match[l]+1][r][1][2]+f[match[l]+1][r][0][2]);
				f[l][r][1][2]=f[l][match[l]][1][0]*(f[match[l]+1][r][0][2]+f[match[l]+1][r][1][2]+f[match[l]+1][r][2][2]);
				f[l][r][2][2]=f[l][match[l]][2][0]*(f[match[l]+1][r][0][2]+f[match[l]+1][r][1][2]+f[match[l]+1][r][2][2]);
				
			}else{
				f[l][r][0][0]=f[l][match[l]][0][1]*f[match[l]+1][r][2][0]+f[l][match[l]][0][2]*f[match[l]+1][r][1][0];
				f[l][r][1][0]=f[l][match[l]][1][0]*(f[match[l]+1][r][1][0]+f[match[l]+1][r][2][0]);
				f[l][r][2][0]=f[l][match[l]][2][0]*(f[match[l]+1][r][1][0]+f[match[l]+1][r][2][0]);
				f[l][r][0][1]=f[match[l]+1][r][0][1]*(f[l][match[l]][0][1]+f[l][match[l]][0][2]);
				f[l][r][1][1]=f[l][match[l]][1][0]*f[match[l]+1][r][0][1];
				f[l][r][2][1]=f[l][match[l]][2][0]*f[match[l]+1][r][0][1];
				f[l][r][0][2]=f[match[l]+1][r][0][2]*(f[l][match[l]][0][1]+f[l][match[l]][0][2]);
				f[l][r][1][2]=f[l][match[l]][1][0]*f[match[l]+1][r][0][2];
				f[l][r][2][2]=f[l][match[l]][2][0]*f[match[l]+1][r][0][2];
			}
		}else{
			SFD(l+1,r-1);
			if(match[l+1]!=r-1){
				f[l][r][1][0]=f[l+1][r-1][0][1]+f[l+1][r-1][2][0]+f[l+1][r-1][0][2]+f[l+1][r-1][0][0]+f[l+1][r-1][2][1]+f[l+1][r-1][2][2];
				f[l][r][2][0]=f[l+1][r-1][0][1]+f[l+1][r-1][1][0]+f[l+1][r-1][0][2]+f[l+1][r-1][0][0]+f[l+1][r-1][1][1]+f[l+1][r-1][1][2];
				f[l][r][0][1]=f[l+1][r-1][1][0]+f[l+1][r-1][0][2]+f[l+1][r-1][2][0]+f[l+1][r-1][0][0]+f[l+1][r-1][1][2]+f[l+1][r-1][2][2];
				f[l][r][0][2]=f[l+1][r-1][1][0]+f[l+1][r-1][0][1]+f[l+1][r-1][2][0]+f[l+1][r-1][0][0]+f[l+1][r-1][1][1]+f[l+1][r-1][2][1];
			}else{
				f[l][r][1][0]=f[l+1][r-1][0][1]+f[l+1][r-1][2][0]+f[l+1][r-1][0][2];
				f[l][r][2][0]=f[l+1][r-1][0][1]+f[l+1][r-1][1][0]+f[l+1][r-1][0][2];
				f[l][r][0][1]=f[l+1][r-1][1][0]+f[l+1][r-1][0][2]+f[l+1][r-1][2][0];
				f[l][r][0][2]=f[l+1][r-1][1][0]+f[l+1][r-1][0][1]+f[l+1][r-1][2][0];
			}
		}
		f[l][r][0][0]%=mol;
		f[l][r][0][1]%=mol;
		f[l][r][0][2]%=mol;
		f[l][r][1][0]%=mol;
		f[l][r][1][1]%=mol;
		f[l][r][1][2]%=mol;
		f[l][r][2][0]%=mol;
		f[l][r][2][1]%=mol;
		f[l][r][2][2]%=mol;
	}
}

longdie的代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 705, mod = 1e9 + 7;
int f[N][N][3][3], match[N];
char s[N];
void dfs(int l, int r) {
	if(l + 1 == r) {
		f[l][r][1][0] = f[l][r][0][1] = 1;
		f[l][r][2][0] = f[l][r][0][2] = 1;
		return;
	}
	if(match[l] == r) {
		dfs(l + 1, r - 1);
		for(int i = 0; i <= 2; ++i) {
			for(int j = 0; j <= 2; ++j) {
				if(i != 1) 
							f[l][r][1][0] += f[l+1][r-1][i][j],
							f[l][r][1][0] %= mod;
				if(j != 1) f[l][r][0][1] += f[l+1][r-1][i][j],
						   f[l][r][0][1] %= mod;
				if(i != 2) f[l][r][2][0] += f[l+1][r-1][i][j],
						   f[l][r][2][0] %= mod;
				if(j != 2) f[l][r][0][2] += f[l+1][r-1][i][j],
						   f[l][r][0][2] %= mod;
			}	
		}
		return;
	}
	register int x = match[l], y = x + 1;
	dfs(l, x), dfs(y, r);
	for(int i = 0; i <= 2; ++i) 
		for(int j = 0; j <= 2; ++j) 
			for(int s = 0; s <= 2; ++s)
				for(int t = 0; t <= 2; ++t) {
					if((s == 1 && t == 1) || (s == 2 && t == 2)) continue;
					f[l][r][i][j] += (1ll*f[l][x][i][s]*f[y][r][t][j]%mod);
					f[l][r][i][j] %= mod;
				}
	
	return;
}
int main() {
	cin >> s + 1;
	int len = strlen(s + 1);
	stack<int> sta;
	for(register int i = 1; i <= len; ++i) {
		if(s[i] == '(') sta.push(i);
		else {
			int x = sta.top(); sta.pop();
			match[x] = i, match[i] = x;
		}
	}
	dfs(1, len);
	int ans = 0;
	for(register int i = 0; i <= 2; ++i)
		for(register int j = 0; j <= 2; ++j) 
			ans = (ans + f[1][len][i][j]) % mod;
	cout << ans << '
';
	return 0;
}
原文地址:https://www.cnblogs.com/614685877--aakennes/p/13885171.html