-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBracket Sequence Deletion.cpp
75 lines (72 loc) · 1.63 KB
/
Bracket Sequence Deletion.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
// https://codeforces.com/contest/1657/problem/C
// Concept used : stack
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define nl cout<<"\n";
bool fun(string s){
string s1 = s;
reverse(s1.begin(),s1.end());
// cout<<"here";
return s1==s;
}
void solve() {
int n=0;
cin>>n;
string s;
cin>>s;
reverse(s.begin(),s.end());
int i = n-1;
int rem = n;
int tot = 0;
int ok=0;
while(i>=0 and s.size()){
stack<int> curr;
int z=0;
string tmp = "";
tmp += s[i];
if(s[i]=='(') curr.push(1);
else curr.push(0);
i--;
int last = s.size();
z++;
int xx=0;
while(i>=0 and s.size() and !curr.empty()){
z++;
tmp = move(tmp) + s[i];
// cout<<i<<" ";
// cout<<tmp<<" ";
if(s[i]=='(') curr.push(1);
else if(!curr.empty() and curr.top()==1) curr.pop();
else curr.push(0);
int zz=0;
if(curr.empty() || (tmp[0]==tmp[tmp.size()-1] and fun(tmp))){
// cout<<"here";
rem-=z;
tot++;
// cout<<z<<" ";
while(z--) s.pop_back();
tmp = "";
zz=1;
xx=1;
}
if(zz and last==s.size()){
ok = 1;
break;
}
i--;
if(tmp=="") break;
// i--;
}
if(ok) break;
}
cout<<tot<<" "<<s.size();nl
}
int32_t main(){
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}