-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJAlgo_2800.java
More file actions
73 lines (63 loc) · 2.37 KB
/
Copy pathBJAlgo_2800.java
File metadata and controls
73 lines (63 loc) · 2.37 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class BJAlgo_2800 {
static ArrayList<ArrayList<Integer[]>> combi = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] equation = br.readLine().split("");
ArrayList<Integer> stack = new ArrayList<>();
ArrayList<Integer[]> brackets = new ArrayList<>();
Set<String> answer = new HashSet<>();
for (int i = 0; i < equation.length; i++) {
if (equation[i].equals("(")) {
equation[i] = "";
stack.add(i);
} else if (equation[i].equals(")")) {
equation[i] = "";
brackets.add(new Integer[]{stack.remove(stack.size() - 1), i});
}
}
for (int i = 0; i < brackets.size(); i++) {
combi.clear();
combination(brackets, new boolean[brackets.size()], 0, i);
for (ArrayList<Integer[]> b : combi) {
String[] result = new String[equation.length];
System.arraycopy(equation, 0, result, 0, result.length);
StringBuilder s = new StringBuilder();
for (Integer[] idx : b) {
result[idx[0]] = "(";
result[idx[1]] = ")";
}
for (String r : result) {
s.append(r);
}
answer.add(s.toString());
}
}
List<String> answerList = new ArrayList(answer);
Collections.sort(answerList);
for (String a : answerList) {
System.out.println(a);
}
}
static void combination(ArrayList<Integer[]> arr, boolean[] visited, int start, int r) {
if(r == 0) {
ArrayList<Integer[]> tmp = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
if (visited[i]) {
tmp.add(arr.get(i));
}
}
combi.add(tmp);
return;
} else {
for(int i = start; i < arr.size(); i++) {
visited[i] = true;
combination(arr, visited, i + 1, r - 1);
visited[i] = false;
}
}
}
}