-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path29.cpp
52 lines (42 loc) · 1.02 KB
/
29.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
// O(t*n) with t is number of test cases and n is length of expression
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
using namespace std;
#define FILE_IO {freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);}
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL)
inline string solve(string &s) {
stack<char> straw;
string res = "";
char top;
for (int i = 0; i < s.size(); ++i) {
if (s[i] >= 'a' && s[i] <= 'z') {
res += s[i];
} else if (s[i] == ')') {
res += straw.top(); straw.pop();
} else if (s[i] != '(')
straw.push(s[i]);
}
while (!straw.empty()) {
res += straw.top(); straw.pop();
}
return res;
}
int main() {
FAST_IO;
// FILE_IO;
int n;
string s;
stack<char> straw;
cin >> n;
while (n--) {
cin >> s;
cout << solve(s) << endl;
}
return 0;
}