#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class Solution { public: long long count(int start, int size); unordered_map cache; string s; }; long long Solution::count(int start, int size) { long long rv; int i; long long key; if (size <= 1) return 0; key = (1LL << start) | (1LL << (start+size)); if (cache.find(key) != cache.end()) return cache[key]; rv = count(start+1, size-1); // Delete the first character. if (s[start] == '(' || s[start] == '[') { for (i = 1; i < size; i++) { if ((s[start] == '(' && s[start+i] == ')') || (s[start] == '[' && s[start+i] == ']') ) { rv += ( (count(start+1, i-1)+1) * (count(start+i+1, size-(i+1))+1) ); } } } cache[key] = rv; return rv; } int main() { Solution sol; cin >> sol.s; cout << sol.count(0, sol.s.size()) << endl; return 0; }