Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<algorithm>
- #include<string>
- #include<vector>
- #include<map>
- #include<queue>
- #include<stack>
- #include<sstream>
- using namespace std;
- #define FOR(i,n) for(i=0;i<(n);i++)
- #define FOR1(i,n) for(i=1;i<=(n);i++)
- #define FORab(i,a,b) for(i=(a);i<=(b);i++)
- #define FORit(it,a) for( typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
- #define all(a) (a).begin(),(a).end()
- #define ms(a,b) memset((a),(b),sizeof(a))
- #define pb push_back
- #define sz size()
- #define fs first
- #define sc second
- #define MOD 1000000007
- typedef vector<int> vi;
- typedef pair<int,int> pi;
- typedef pair< int, pi > pii;
- typedef long long LL;
- LL mem[707][707][4][4],match[707];
- LL solve(int s,int e,int lc,int rc)
- {
- if(s>e) return 1;
- if(mem[s][e][lc][rc]!=-1)return mem[s][e][lc][rc];
- LL ret=0,i,j;
- if(match[s]==e)
- {
- FOR1(i,2)
- {
- {
- if(lc!=i)ret+=solve(s+1,e-1,i,0);
- ret%=MOD;
- if(rc!=i)ret+=solve(s+1,e-1,0,i);
- ret%=MOD;
- }
- }
- }
- else
- {
- FOR1(i,2)
- {
- FOR1(j,2)
- {
- if(lc!=i)
- {
- ret+=(solve(s+1,match[s]-1,i,0)*solve(match[e]+1,e-1,j,0))%MOD;
- ret%=MOD;
- }
- if(lc!=i&&rc!=j)
- {
- ret+=(solve(s+1,match[s]-1,i,0)*solve(match[e]+1,e-1,0,j))%MOD;
- ret%=MOD;
- }
- if(i!=j)
- {
- ret+=(solve(s+1,match[s]-1,0,i)*solve(match[e]+1,e-1,j,0))%MOD;
- ret%=MOD;
- }
- if(rc!=j)
- {
- ret+=(solve(s+1,match[s]-1,0,i)*solve(match[e]+1,e-1,0,j))%MOD;
- ret%=MOD;
- }
- }
- }
- }
- // cout<<s<<" "<<e<<" "<<lc<<" "<<rc<<" "<<ret<<" "<<match[s]<<" "<<match[e]<<endl;
- return mem[s][e][lc][rc]=ret;
- }
- int main()
- {
- ms(mem,-1);
- string str;
- int i,j,x;
- cin>>str;
- FOR(i,str.sz)
- {
- if(str[i]=='(')
- {
- x=0;
- FORab(j,i+1,str.sz-1)
- {
- if(str[j]=='(')x++;
- else
- {
- if(x==0)
- {
- match[i]=j;
- match[j]=i;
- break;
- }
- else x--;
- }
- }
- }
- // cout<<match[i]<<endl;
- }
- cout<<solve(0,str.size()-1,0,0)<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment