-
Notifications
You must be signed in to change notification settings - Fork 1
/
14_Remove_Parentheses.cpp
66 lines (55 loc) · 1.04 KB
/
14_Remove_Parentheses.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
#include<iostream>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
set<string> results;
int startPos[10];
int endPos[10];
int pCount;
void GetParenthese(string input, int index, bool isRemove)
{
if (index >= pCount)
{
input.erase(remove(input.begin(), input.end(), ' '), input.end());
results.insert(input);
}
else
{
if (isRemove)
{
input.replace(startPos[index], 1, " ");
input.replace(endPos[index], 1, " ");
}
GetParenthese(input, index + 1, true);
GetParenthese(input, index + 1, false);
}
}
int main()
{
string input;
cin >> input;
pCount = count(input.begin(), input.end(), '(');
stack<int> stack;
for (int i = 0, count = 0; i < input.size(); ++i)
{
if (input[i] == '(')
{
startPos[count] = i;
stack.push(count++);
}
else if (input[i] == ')')
{
endPos[stack.top()] = i;
stack.pop();
}
}
GetParenthese(input, 0, true);
GetParenthese(input, 0, false);
for (string str : results)
{
if (str == input)continue;
printf("%s\n", str.c_str());
}
}
// ((((a+b)))/(d))