-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4882.cpp
90 lines (80 loc) · 1.28 KB
/
4882.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
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string input;
int top;
int stack1[3000];
bool print[3000];
bool stack2[3000];
void solve();
void process();
int main()
{
while(getline(cin, input))
{
solve();
}
return 0;
}
void solve()
{
top = 0;
memset(print, true, sizeof print);
process();
for(int i = 0; i < input.size(); ++i)
{
if(print[i])
{
cout << input[i];
}
}
cout << endl;
}
void process()
{
for(int i = 0; i < input.size(); i++)
{
if(input[i] == '(')
{
stack1[top] = i;
stack2[top] = false;
top++;
}
else
{
if(input[i] == '+')
{
if(top)
{
stack2[top - 1] = true;
}
}
else
{
if(input[i] == ')')
{
if(((stack2[top - 1] && (stack1[top - 1] - 1 >= 0 && input[stack1[top - 1] - 1] != '+' && input[stack1[top - 1] - 1] != '(') || (i + 1 < input.size() && input[i + 1] != '+' && input[i + 1] != ')')) == 0))
{
if(stack1[top - 1] - 1 >= 0)
{
if(input[stack1[top - 1] - 1] == '(')
{
if(i + 1 < input.size())
{
if(input[i + 1] == ')')
{
stack2[top - 2] = stack2[top - 1];
}
}
}
}
print[stack1[top - 1]] = false;
print[i] = false;
}
top--;
}
}
}
}
}