1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using namespace std;

#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;

bool dp[100][100];
int prefA[100], prefB[100];

bool CheckSlow(const string &A, const string &B) {
	const int n = SZ(A);
	const int m = SZ(B);

	for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) dp[i][j] = false;
	dp[0][0] = true;

	prefA[0] = prefB[0] = 0;
	for (int i = 0; i < n; ++i) {
		prefA[i + 1] = prefA[i] + (A[i] == '(' ? 1 : -1);
	}
	for (int i = 0; i < m; ++i) {
		prefB[i + 1] = prefB[i] + (B[i] == '(' ? 1 : -1);
	}

	if (prefA[n] + prefB[m] != 0) { return false; }

	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= m; ++j) {
			if (prefA[i] + prefB[j] < 0) { continue; }
			dp[i][j] |= (i && dp[i - 1][j]);
			dp[i][j] |= (j && dp[i][j - 1]);
		}
	}

	return dp[n][m];
}

vector<pii> MakePattern(const string &S) {
	vector<pii> ans{{0, 0}};
	int delta = 0;
	int max_delta = 0, min_delta = 0;

	for (char ch : S) {
		delta += (ch == '(' ? 1 : -1);
		if (delta > max_delta) {
			max_delta = delta;
			if (ans.back().second == min_delta) {
				ans.back().first = delta;
			} else {
				ans.emplace_back(max_delta, min_delta);
			}
		}
		if (delta < min_delta) {
			min_delta = delta;
		}
	}
	ans.emplace_back(max_delta, min_delta);
	return ans;
}

bool CheckFastWrap(const string &A, const string &B) {
	const int deltaA = count(ALL(A), '(') - count(ALL(A), ')');
	const int deltaB = count(ALL(B), '(') - count(ALL(B), ')');
	if (deltaA + deltaB != 0) { return false; }

	const auto ptn_A = MakePattern(A);
	const auto ptn_B = MakePattern(B);
	int ptr_A = 0, ptr_B = 0;
	debug(ptn_A, ptn_B);

	while (ptr_A < SZ(ptn_A) - 1 || ptr_B < SZ(ptn_B) - 1) {
		if (ptr_A < SZ(ptn_A) - 1 && ptn_A[ptr_A + 1].second + ptn_B[ptr_B].first >= 0) {
			++ptr_A;
		} else if (ptr_B < SZ(ptn_B) - 1 && ptn_B[ptr_B + 1].second + ptn_A[ptr_A].first >= 0) {
			++ptr_B;
		} else {
			return false;
		}
	}

	return true;
}

void Rev(string &S) {
	reverse(ALL(S));
	for (char &ch : S) { ch ^= '(' ^ ')'; }
}

bool CheckFast(string A, string B) {
	if (!CheckFastWrap(A, B)) { return false; }
	Rev(A); Rev(B);
	if (!CheckFastWrap(A, B)) { return false; }
	return true;
}

string ReadLine() {
	int n;
	string s;
	cin >> n >> s;
	char ch = s[0];

	s = "";
	for (int i = 0; i < n; ++i) {
		int nrep;
		cin >> nrep;
		s += string(nrep, ch);
		ch ^= '(' ^ ')';
	}
	return s;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(14);
	cerr << fixed << setprecision(6);

	string A = ReadLine();
	string B = ReadLine();

	const int delta_A = count(ALL(A), '(') - count(ALL(A), ')');

	i64 ans = 0;
	for (int i = 0; i < SZ(B); ++i) {
		int delta_B = 0;
		for (int len = 1; i + len <= SZ(B); ++len) {
			delta_B += B[i + len - 1] == '(' ? 1 : -1;
			if (delta_A + delta_B != 0) {
				continue;
			}
			// string Q = B.substr(i, len);
			ans += CheckFast(A, B.substr(i, len));
		}
	}
	cout << ans << "\n";
}