Coloring Brackets CodeForces

来源:互联网 发布:新兵的爆菊经历知乎 编辑:程序博客网 时间:2024/05/28 11:48

分析:

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

但是我逗比地用了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));    }}

0 0
原创粉丝点击