Coloring Brackets CodeForces - 149D 区间dp

分析:

自己也想到了用四维数组存

但是我逗比地用了dp[l][r]=dp[l+1][r]+dp[l][r-1];。、

还是做题少。。也许我还是应该多看看题解。。(要不然想错了。。然后自己没有锻炼到能力,又白白走神)

只不过重点是看过题解之后自己能不看代码敲出来;

或者看了代码之后能不看代码敲出来。。这样就理解了。。






再次低效。。

有时候觉得还会自己比较活跃的时候想这些题目比较好。。

要不然想不出来就会傻等。就会白白走神浪费时间。


、、这是别人的代码的。  是通过枚举左右两边是什么颜色来缩短区间

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
#include<sstream>
#include<queue>
#define bug1 cout<<"bug1"<<endl;
using namespace std;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a));
const int maxn=705;
char s[maxn];
const int mod=1e9+7;
int match[maxn];
LL d[maxn][maxn][3][3];
LL dp(int l,int r,int lc,int rc){
    if(l>r)return 1;
    LL &ans=d[l][r][lc][rc];
    if(ans>=0)return ans;
    ans=0;
    int mr=match[l];
    for(int c=1;c<=2;++c){
        if(mr<r||c!=rc)
            ans=(ans+dp(l+1,mr-1,0,c)*dp(mr+1,r,c,rc))%mod;
        if(c!=lc)
            ans=(ans+dp(l+1,mr-1,c,0)*dp(mr+1,r,0,rc))%mod;
    }
    return ans;
}
vector <int>a;
int main(){
    while(~sf("%s",s+1)){
            mem(d,-1);
        int n=strlen(s+1);
        int top=0;
        for(int i=1;i<=n;++i){
            if(s[i]=='('){
                a.push_back(i);
            }
            else{
                match[i]=a.back();
                match[a.back()]=i;
                a.pop_back();
            }
        }
        pf("%lld\n",dp(1,n,0,0));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值