救救孩子
查看原帖
救救孩子
379420
Xuejiama1227楼主2024/4/7 23:21
// Problem: Coloring Brackets
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF149D
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define Rep(i,x,y) for(int i=(int)(x);i<=(int)(y);++i)
#define Per(i,x,y) for(int i=(int)(x);i>=(int)(y);--i)
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)<(y)?(y):(x))
#define DEBUG 1
namespace FastIO{
#define isdigit(x) (x>='0'&&x<='9')
#define MAXSIZE (1<<20)
#define PREC 6
#define FileIO(fn) freopen(fn".in","r",stdin),freopen(fn".out","w",stdout)
	char buf[MAXSIZE],pbuf[MAXSIZE],*pp=pbuf,*p1,*p2;
	struct CONDES{
#if DEBUG
#else
		CONDES(){p1=buf;p2=buf;pp=pbuf;}
		~CONDES(){fwrite(pbuf,1,pp-pbuf,stdout);}
#endif
	}_CONDES;
	inline char gc(){
#if DEBUG
		return getchar();
#endif
		if(p1==p2)p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
		return p1==p2?' ':*p1++;
	}
	template<class T>
	inline void read(T&x){
		double tmp=1;
		bool sign=0;x=0;
		char c=gc();
		for(;!isdigit(c);c=gc())if(c=='-')sign=1;
		for(;isdigit(c);c=gc())x=x*10+(c-'0');
		if(c=='.')for(c= gc();isdigit(c);c=gc())tmp/=10.0,x+=tmp*(c-'0');
		if(sign)x=-x;
	}
	inline bool blank(char ch){
		return ch==' '||ch=='\n'||ch=='\r'||ch=='\t'||ch==EOF;
	}
	inline void read(char *s){
		char c=gc();
		for(;blank(c);c=gc());
		for(;!blank(c);c=gc())*s++=c;
		*s=0;
	}
	inline void read(char &c){for(c=gc();blank(c);c=gc());}
	template<class T,class...Args>
	inline void read(T&x,Args&...args){read(x);read(args...);}
	inline void pc(const char &c){
#if DEBUG
		putchar(c);
#else
		if(pp-pbuf==MAXSIZE)fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;
		*pp++=c;
#endif
	}
	template<class T>
	inline void write(T x){
		if(x<0)x=-x,pc('-');
		static int sta[35];
		int top=0;
		do{sta[top++]=x%10,x/=10;}while(x);
		while(top)pc(sta[--top]+'0');
	}
	inline void write(char* s){while(*s)pc(*s++);}
	inline void write(const char* s){while(*s)pc(*s++);}
	inline void write(char c){pc(c);}
	template<class T>
	inline void write_float(T x,int prec=PREC){
		if(x==0){pc('0');pc('.');while(prec--)pc('0');return;}
		if(x<0)x=-x,pc('-');
		static int sta[35];
		int top=0;T pw=pow(10,prec);
		x=round(pw*x);
		if(x<pw)pc('0'),pc('.');
		do{sta[top++]=((ull)x)%10;x=(x-(ull)x%10)/10.0;}while(x>=1.0);
		while(top){pc(sta[--top]+'0');if(top==prec)pc('.');}
	}
	inline void write(float x){write_float(x);}
	inline void write(double x){write_float(x);}
	inline void write(long double x){write_float(x);}
	template<class T,class...Args>
	inline void write(T x,Args...args){write(x);write(args...);}
};
using namespace FastIO;
const int N=710,MOD=1e9+7;
struct node{
	int l,r,fa,dp[3][3];
	vector<int>sn;
}a[N];
int n,f[N][3][3],nf[N][3][3];
char c[N];
void solve(int x){
	int k,i,j,i1,j1;
	if(!a[x].sn.size()){
		for(i=0;i<=2;i++)for(j=0;j<=2;j++)
			if(!i&&!j)a[x].dp[i][j]=1;
			else a[x].dp[i][j]=0;
		return;
	}
	for(k=0;k<a[x].sn.size();k++){
		solve(a[x].sn[k]);
		for(i=0;i<=2;i++)for(j=0;j<=2;j++){
			f[k][i][j]=0;
			if(!i&&!j||i&&j)continue;
			else if(i){
				for(i1=0;i1<=2;i1++)for(j1=0;j1<=2;j1++)if(i1^i)
					f[k][i][j]=(f[k][i][j]+a[a[x].sn[k]].dp[i1][j1])%MOD;
			}else{
				for(i1=0;i1<=2;i1++)for(j1=0;j1<=2;j1++)if(j1^j)
					f[k][i][j]=(f[k][i][j]+a[a[x].sn[k]].dp[i1][j1])%MOD;	
			}
		}
	}
	for(i=0;i<=2;i++)for(j=0;j<=2;j++)nf[0][i][j]=f[0][i][j];
	for(k=1;k<a[x].sn.size();k++)
		for(i=0;i<=2;i++)for(j=0;j<=2;j++){
			nf[k][i][j]=0;
			for(i1=0;i1<=2;i1++)for(j1=0;j1<=2;j1++)if(!i1||!j1||i1^j1)
				nf[k][i][j]=nf[k][i][j]+(1ll*nf[k-1][i][i1]*f[k][j1][j]%MOD)%MOD;
		}
	for(i=0;i<=2;i++)for(j=0;j<=2;j++)a[x].dp[i][j]=nf[a[x].sn.size()-1][i][j];
}
signed main(){
	int i,j,t=0,nc=0,ans=0;
	read(c+1);n=strlen(c+1);
	a[0].l=0;a[0].r=n+1;
	for(i=1;i<=n;i++){
		if(c[i]=='('){
			a[t].sn.push_back(++nc);
			a[nc].l=i;a[nc].fa=t;
			t=nc;
		}else{
			a[t].r=i;
			t=a[t].fa;
		}
	}
	solve(0);
	for(i=0;i<=2;i++)for(j=0;j<=2;j++)ans=(ans+a[0].dp[i][j])%MOD;
	write(ans);
	return 0;
}

WA on test 7

2024/4/7 23:21
加载中...