# CF149D Coloring Brackets

## 代码

#include<bits/stdc++.h>
using namespace std;
#define re register
#define in inline
#define ll long long
#define get getchar()
#define right ri
#define left le
#define int ll
const int _=755;
const int mod=1e9+7;
int st[_];
int n,top,left[_],right[_];
char s[_];
ll dp[_][_][3][3];
in bool check(int l,int r,int x,int y)
{
if(right[l]!=r) return 1;
if(x>0&&y>0) return 0;
if(x==0 && y==0) return 0;
return 1;
} //判断l,r是否合法(若l,r无关系则必定合法)
in int dfs(int l,int r,int x,int y)
{
if(dp[l][r][x][y]) return dp[l][r][x][y];
if(!check(l,r,x,y)) return 0;
if(l+1==r){
dp[l][r][x][y]=1;
return 1;
}
for(re int i=0;i<=2;i++)
for(re int j=0;j<=2;j++) {
if(left[r]==l) {
if((j>0 && j==y) || (i>0 && x==i)) continue;
dp[l][r][x][y]=(dp[l][r][x][y]+dfs(l+1,r-1,i,j))%mod;
}
else {
if((!check(left[r],r,j,y)) || (!check(l,left[r]-1,x,i))) continue;
if(j==i && i>0) continue;
dp[l][r][x][y]=(dp[l][r][x][y]+dfs(left[r],r,j,y)*dfs(l,left[r]-1,x,i)%mod)%mod;
}
}
return dp[l][r][x][y];
}
signed main()
{
scanf("%s",s+1);
n=strlen(s+1);
st[top]=1;
for(re int i=2;i<=n;i++) {
if(s[i]==')')
right[st[top]]=i,left[i]=st[top--];
else st[++top]=i;
} //预处理每个括号对应的括号的位置
ll ans=0;
for(re int i=0;i<=2;i++)
for(re int j=0;j<=2;j++) {
ans=(ans+dfs(1,n,i,j))%mod;
}
cout<<ans<<endl;
}

posted @ 2020-10-31 22:34  yzhx  阅读(113)  评论(0编辑  收藏  举报