Artifact Content
Not logged in

Artifact ceaca236de3383b6ce0c3b53b44ea369ba871bd0


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class BracketSequenceDiv1 { public:
	long long count(string s)
	{
		return count_multi(s, 0, s.size()) - 1;
	}

	map<pair<int,int>, LL> memo1;
	LL count_multi(const string& str, int s, int e) {
		if(s == e)
			return 1;
		pair<int,int> key(s,e);
		if(memo1.count(key))
			return memo1[key];

		LL total = 1;
		for(int m=s+2; m<=e; ++m)
			total += count_oneblock(str, s, m) * count_multi(str, m, e);
		return memo1[key] = total;
	}

	map<pair<int,int>, LL> memo2;
	LL count_oneblock(const string& str, int s, int e) {
		if(s == e)
			return 0;
		if(str[e-1]=='(' || str[e-1]=='[')
			return 0;
		const char op = (str[e-1]==')' ? '(' : '[');

		pair<int,int> key(s,e);
		if(memo2.count(key))
			return memo2[key];

		LL total = 0;
		for(int k=s; k<e; ++k)
			if(str[k] == op)
				total += count_multi(str, k+1, e-1);
		return memo2[key] = total;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BracketSequenceDiv1().count(s));}
int main(){

CASE(0)
	string s = "()[]"; 
	long long _ = 3LL; 
END
CASE(1)
	string s = "())"; 
	long long _ = 2LL; 
END
CASE(2)
	string s = "()()"; 
	long long _ = 4LL; 
END
CASE(3)
	string s = "([)]"; 
	long long _ = 2LL; 
END
CASE(4)
	string s = "())[]][]([]()]]()]]]"; 
	long long _ = 3854LL; 
END
/*
CASE(5)
	string s = ; 
	long long _ = LL; 
END
CASE(6)
	string s = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE