-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPrint Bracket Number
50 lines (43 loc) · 1.08 KB
/
Print Bracket Number
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
/*
Given an expression exp of length n consisting of some brackets. The task is to print the bracket numbers when the expression is being parsed.
Input:
The first line contains an integer T, the number of test cases. For each test case, there is a string exp containing the expression.
Output:
For each test case, the output is the bracket numbers of the expression.
Constraints:
1<=T<=100
1<=S<=100
Example:
Input:
3
(a+(b*c))+(d/e)
((())(()))
((((()
Output:
1 2 2 1 3 3
1 2 3 3 2 4 5 5 4 1
1 2 3 4 5 5
Explanation:
Test case 1:The highlighted brackets in the given expression (a+(b*c))+(d/e) has been assigned the numbers as: 1 2 2 1 3 3.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t; while(t--){
string s; cin>>s;
int c=1;
stack<pair<char, int>> st;
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
st.push({'(' ,c++});
cout<<st.top().second<<" ";
}
else if(s[i] == ')'){
cout<<st.top().second<<" ";
st.pop();
}
}
cout<<'\n';
}
return 0;
}