fq.cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define PII pair<int, int>
#define MP make_pair
#define PB push_back
#define SIZE(s) (int)(s).int()
#define ll long long
#define INF -1
#define MIL 1000000
#define MIL2 10000000
char s[1001];
#define MAX 1005
#define MAX2 505
map<ll, int> M;
ll toH(int a, int b, int c)
{
return ((ll) c) + (ll)b*1000L + (ll)a * 1000000L;
}
int poc(int p, int o, int z)
{
ll index = toH(p,o,z);
if (M.find(index) != M.end())
return M[index];
if(z>o)
{
M[index] = INF;
return INF;
}
if(s[p] == '(')
o++;
if(s[p] == ')')
z++;
if( p == (int) strlen(s) -1)
{
if(s[p] == '.')
z++;
if(z != o || s[p] == '(')
{
M[index] = INF;
return INF;
}
else
{
M[index] = 1;
return 1;
}
}
if(s[p] == '.')
{
int b = 0;
int oo = poc(p+1,o+1,z);
// printf("%d,%d,%d %d\n",p+1,o+1,z,oo);
int zz = poc(p+1,o,z+1);
// printf("%d,%d,%d %d\n",p+1,o,z+1,zz);
if(oo != INF)
b += oo;
if(zz != INF)
b += zz;
// printf("p=%d o=%d z=%d oo=%d zz=%d b=%d\n",p,o,z,oo,zz,b);
M[index] = b % MIL2;
return b % MIL2;
}
else
{
int c = poc(p+1,o,z);
M[index] = c % MIL2;
return c% MIL2;
}
}
int main()
{
while(gets(s))
{
M.clear();
int g = poc(0,0,0);
printf("%d\n",(g == -1) ? 0 : g%MIL);
}
return 0;
}