-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANKPAREN.cpp
executable file
·96 lines (95 loc) · 1.93 KB
/
ANKPAREN.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
bool check(string given){
stack<char> st;
for(auto i:given){
if(st.empty() or i == '('){
st.push(i);
}
else{
if(st.top() == '('){
st.pop();
}
else{
st.push(i);
}
}
}
return st.empty();
}
void solve(string given, lli k){
lli tillNow = 0;
lli i = 1;
if(given[0] == ')'){
tillNow++;
}
while(k != tillNow and i<given.size()){
if(given[i] != given[i-1] and given[i] == ')'){
tillNow++;
if(tillNow == k){
break;
}
}
i++;
}
if(k == tillNow){
for(lli j = 0;j<given.size();j++){
if(j != i){
cout<<given[j];
}
}
return ;
}
i = given.size()-2;
if(given.back() == '('){
tillNow++;
}
while(k != tillNow and i>= 0){
if(given[i] != given[i+1] and given[i] == '('){
tillNow++;
if(tillNow == k){
break;
}
}
i--;
}
if(tillNow != k){
cout<<-1;
}
else{
for(lli j = 0;j<given.size();j++){
if(j != i){
cout<<given[j];
}
}
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/home/cpp/inputf.in", "r", stdin);
freopen("/home/home/cpp/outputf.in", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
lli t;cin>>t;
while(t--){
string given;
cin>>given;
lli k;
cin>>k;
if(check(given)){
solve(given, k);
}
else{
if(k == 1){
cout<<given;
}
else{
cout<<-1;
}
}
cout<<endl;
}
return 0;
}