-
Notifications
You must be signed in to change notification settings - Fork 1
/
C.cpp
77 lines (61 loc) · 1.45 KB
/
C.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
map <ll, ll> lst;
string ara[300009];
ll full = 0;
bool mark[300009], f[300009];
ll frst[300009];
char in[300009];
int main()
{
ll n;
cin >> n;
for(ll i = 0; i < n; i++) {
scanf("%s", in);
ara[i] = string(in, strlen(in));
}
for(ll i = 0; i < n; i++) {
ll counter = 0;
stack <char> st;
for(ll j = 0; j < ara[i].length(); j++) {
if(ara[i][j] == '(')
st.push(ara[i][j]);
else {
if(st.empty()) {
counter++;
continue;
}
if(st.top() == '(')
st.pop();
}
}
if(st.empty() && counter == 0) {
mark[i] = 1;
full++;
}
else if(st.empty() && counter > 0) {
lst[counter]++;
}
else if(!st.empty() && counter == 0) {
frst[i] = st.size();
//cout << st.size() << " " << i << endl;
f[i] = 1;
}
else {
mark[i] = 1;
}
}
ll ans = full * full;
//cout << ans << endl;
for(ll i = 0; i < n; i++) {
if(mark[i])
continue;
if(!f[i])
continue;
ans += (lst[ frst[i] ]);
//cout << frst[i] << " " << lst[ frst[i] ] << " " << i << endl;
}
cout << ans << endl;
return 0;
}