-
Notifications
You must be signed in to change notification settings - Fork 0
/
괄호 제거.java
75 lines (54 loc) · 1.66 KB
/
괄호 제거.java
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
import java.io.*;
import java.util.*;
public class Main {
static char[] arr;
static int[] pair;
static TreeSet<String> set;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
arr = input.toCharArray();
pair = new int[arr.length];
Stack<Integer> stack = new Stack<>();
for(int i =0; i<arr.length; i++) {
if(arr[i] == '(') {
stack.push(i);
}
else if(arr[i] ==')') {
pair[i] =stack.peek();
pair[stack.peek()] =i;
stack.pop();
}
}
set = new TreeSet();
visited = new boolean[arr.length];
dfs(0,arr.length);
set.remove(input);
StringBuilder sb = new StringBuilder();
set.stream().forEach(ans -> sb.append(ans).append("\n"));
System.out.println(sb);
}
static void dfs(int idx, int len ) {
if(idx == len) {
print();
return;
}
if(arr[idx]=='(') {
visited[idx] =true;
visited[pair[idx]] =true;
dfs(idx+1,len);
visited[idx]= false;
visited[pair[idx]]= false;
}
dfs(idx+1,len);
}
static void print() {
String tmp = "";
for(int i = 0;i <arr.length; i++) {
if(!visited[i])
tmp +=arr[i];
}
set.add(tmp);
}
}