区间DP,莫名原因RE/WA on 样例2
查看原帖
区间DP,莫名原因RE/WA on 样例2
443675
紊莫turtle楼主2023/2/11 17:13

vscode跑不出来,但是洛谷IDE输出10,和CF上一样。

//Author: Velvet on Luogu(uid=443675)
#include <bits/stdc++.h>
#define int long long
#define mkpr make_pair
#define fi first
#define se second
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define dF(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
using namespace __gnu_cxx;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(int x){write(x);putchar('\n');}
inline void writesp(int x){write(x);putchar(' ');}
inline int lowbit(int x) {return x&(-x);}
typedef pair<int,int> Pair;
const int N=705,P=1000000007;
int n,f[N][N][3][3],R[N];
string s;
void dp(int l,int r){
	if(l+1==r){
		f[l][r][0][1]=f[l][r][0][2]=f[l][r][1][0]=f[l][r][2][0]=1;
	}else if(R[l]==r){
		dp(l+1,r-1);
		F(i,0,2) F(j,0,2){
			if(j!=1) (f[l][r][0][1]+=f[l+1][r-1][i][j]%P)%=P;
			if(j!=2) (f[l][r][0][2]+=f[l+1][r-1][i][j]%P)%=P;
			if(i!=1) (f[l][r][1][0]+=f[l+1][r-1][i][j]%P)%=P;
			if(i!=2) (f[l][r][2][0]+=f[l+1][r-1][i][j]%P)%=P;
		}
	}else{
		dp(l,R[l]);dp(R[l]+1,r);
		F(i,0,2) F(j,0,2) F(k,0,2) F(q,0,2){
			if(j!=k||!j&&!k){
				(f[l][r][i][q]+=f[l][R[l]][i][j]*f[R[l]+1][r][k][q]%P)%=P;
			}
		}
	}
}
signed main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>s; n=s.size(); s=" "+s;
	stack <int> st;
	F(i,1,n){
		if(s[i]=='(') st.push(i);
		else R[st.top()]=i,st.pop();
	}
	dp(1,n);
	int ans=0;
	F(i,0,2) F(j,0,2) {
		ans+=f[1][n][i][j];ans%=P;
	}cout<<ans;
    return 0;
}

2023/2/11 17:13
加载中...