Guest User

Untitled

a guest
Jun 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include<string.h>
  5. #include<algorithm>
  6. #include<string>
  7. #include<vector>
  8. #include<map>
  9. #include<queue>
  10. #include<stack>
  11. #include<sstream>
  12. using namespace std;
  13. #define FOR(i,n) for(i=0;i<(n);i++)
  14. #define FOR1(i,n) for(i=1;i<=(n);i++)
  15. #define FORab(i,a,b) for(i=(a);i<=(b);i++)
  16. #define FORit(it,a) for( typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
  17. #define all(a) (a).begin(),(a).end()
  18. #define ms(a,b) memset((a),(b),sizeof(a))
  19. #define pb push_back
  20. #define sz size()
  21. #define fs first
  22. #define sc second
  23. #define MOD 1000000007
  24. typedef vector<int>   vi;
  25. typedef pair<int,int>  pi;
  26. typedef pair< int, pi >  pii;
  27. typedef long long  LL;
  28. LL mem[707][707][4][4],match[707];
  29. LL solve(int s,int e,int lc,int rc)
  30. {
  31.    if(s>e) return 1;
  32.    if(mem[s][e][lc][rc]!=-1)return mem[s][e][lc][rc];
  33.    LL ret=0,i,j;
  34.    if(match[s]==e)
  35.    {
  36.        FOR1(i,2)
  37.        {
  38.            {
  39.                 if(lc!=i)ret+=solve(s+1,e-1,i,0);
  40.                 ret%=MOD;
  41.                 if(rc!=i)ret+=solve(s+1,e-1,0,i);
  42.                 ret%=MOD;
  43.  
  44.            }
  45.        }
  46.  
  47.    }
  48.    else
  49.    {
  50.         FOR1(i,2)
  51.         {
  52.             FOR1(j,2)
  53.             {
  54.                 if(lc!=i)
  55.                 {
  56.                     ret+=(solve(s+1,match[s]-1,i,0)*solve(match[e]+1,e-1,j,0))%MOD;
  57.                     ret%=MOD;
  58.                 }
  59.                 if(lc!=i&&rc!=j)
  60.                 {
  61.                     ret+=(solve(s+1,match[s]-1,i,0)*solve(match[e]+1,e-1,0,j))%MOD;
  62.                     ret%=MOD;
  63.                 }
  64.                 if(i!=j)
  65.                 {
  66.                     ret+=(solve(s+1,match[s]-1,0,i)*solve(match[e]+1,e-1,j,0))%MOD;
  67.                     ret%=MOD;
  68.                 }
  69.                 if(rc!=j)
  70.                 {
  71.                     ret+=(solve(s+1,match[s]-1,0,i)*solve(match[e]+1,e-1,0,j))%MOD;
  72.                     ret%=MOD;
  73.                 }
  74.             }
  75.         }
  76.  
  77.    }
  78.   // cout<<s<<" "<<e<<" "<<lc<<" "<<rc<<" "<<ret<<" "<<match[s]<<" "<<match[e]<<endl;
  79.    return mem[s][e][lc][rc]=ret;
  80. }
  81. int main()
  82. {
  83.     ms(mem,-1);
  84.     string str;
  85.     int i,j,x;
  86.     cin>>str;
  87.     FOR(i,str.sz)
  88.     {
  89.         if(str[i]=='(')
  90.         {
  91.             x=0;
  92.             FORab(j,i+1,str.sz-1)
  93.            {
  94.                if(str[j]=='(')x++;
  95.                else
  96.                {
  97.                    if(x==0)
  98.                    {
  99.                        match[i]=j;
  100.                        match[j]=i;
  101.                        break;
  102.                    }
  103.                    else x--;
  104.                }
  105.            }
  106.         }
  107.       //  cout<<match[i]<<endl;
  108.     }
  109.     cout<<solve(0,str.size()-1,0,0)<<endl;
  110.     return 0;
  111. }
Add Comment
Please, Sign In to add comment