-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbieu_thuc_tuong_duong.cpp
More file actions
79 lines (79 loc) · 1.62 KB
/
bieu_thuc_tuong_duong.cpp
File metadata and controls
79 lines (79 loc) · 1.62 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;
long long mod = 1e9 + 7;
string Reverse_Sign(string tmp)
{
for (int i = 0; i < (int)tmp.size(); i++)
{
if (tmp[i] == '-')
tmp[i] = '+';
else if (tmp[i] == '+')
tmp[i] = '-';
}
return tmp;
}
void slove()
{
string s;
cin >> s;
if (isalpha(s[0]) || s[0] == '(')
s.insert(0, 1, '+');
for (int i = 1; i < (int)s.size(); i++)
{
if ((s[i] == '(' && s[i - 1] == '(') || (isalpha(s[i]) && s[i - 1] == '('))
{
s.insert(i, 1, '+');
i--;
}
}
stack<string> stk;
for (int i = 0; i < (int)s.size(); i++)
{
if (s[i] == ')')
{
string tmp = "";
while (!stk.empty() && stk.top() != "(")
{
tmp = stk.top() + tmp;
stk.pop();
}
stk.pop();
if (stk.top() == "-")
tmp = Reverse_Sign(tmp);
stk.pop();
stk.push(tmp);
}
else
{
string tmp(1, s[i]);
stk.push(tmp);
}
}
string ans = "";
while (!stk.empty())
{
ans = stk.top() + ans;
stk.pop();
}
while (ans.front() == '+')
ans.erase(0, 1);
cout << ans << "\n";
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
slove();
}
return 0;
}