D. Neko and Aki's Prank(记忆化搜索)

D. Neko and Aki's Prank

 

思路:

可以模拟一下题意,就是建立一个树,树的(的数量大于等于)的数量。

数据较大,所以可以建立二维数组,记录每次遇到的情况,dp[i][j][k]数组表示i个左括号,j个右括号,

选取的状态为k时的满足已选择的边的数量。

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL; 
const int maxn = 1002;
const LL MOD = 1e9+7;
int n;
LL dp[maxn][maxn][2]={0};
LL dfs(int c1,int c2,int fg){
	if(c2==n) return 0;
	if(dp[c1][c2][fg]) return dp[c1][c2][fg];
	LL ans = 0;
	if(c1<n){
		int k = !fg;
		if(fg==0) ans = (1+dfs(c1+1,c2,k))%MOD;//fg=0表示下一次可以选择,数量+1 
		else ans = dfs(c1+1,c2,k)%MOD;
	}
	if(c2<c1){
		int k = !fg;
		if(fg==0) ans = (ans+1+dfs(c1,c2+1,k))%MOD;
		else ans = (ans+dfs(c1,c2+1,k))%MOD;
	}
	dp[c1][c2][fg] = ans;
	return ans;
}
int main(void)
{
	scanf("%d",&n);
	printf("%lld\n",(1+dfs(1,0,1))%MOD);
	return 0; 
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值