|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main{ |
| 8 | + private static int N; |
| 9 | + public static void main(String[] args) throws IOException { |
| 10 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + // 타겟 : x, y |
| 12 | + // x1, 가 등장하면, x1까지의 stack에 모두 표식 |
| 13 | + // y1, y2가 등장한다면 이제 stack에서 pop될때 표식 되있는것이라면 그것 출력 |
| 14 | + |
| 15 | + N = Integer.parseInt(br.readLine()); |
| 16 | + |
| 17 | + String arr = br.readLine(); |
| 18 | + char[] cs = arr.toCharArray(); |
| 19 | + String temp1 = br.readLine(); |
| 20 | + String[] temp = temp1.split(" "); |
| 21 | + int x = Integer.parseInt(temp[0]) - 1; |
| 22 | + int y = Integer.parseInt(temp[1]) - 1; |
| 23 | + Deque<int[]> stack = new ArrayDeque<>(); |
| 24 | + |
| 25 | + int[][] indexesAtname = new int[N][2]; |
| 26 | + |
| 27 | + int name = 0; |
| 28 | + boolean isFindMode = false; |
| 29 | + int[] popElement = null; |
| 30 | + |
| 31 | + for(int i = 0; i < arr.length(); i++) { |
| 32 | + char c = cs[i]; |
| 33 | + if(c == '0'){ |
| 34 | + // 넣기 |
| 35 | + indexesAtname[name][0] = i; |
| 36 | + stack.push(new int[]{name++, 0}); |
| 37 | + }else{ |
| 38 | + // 빼기 |
| 39 | + popElement = stack.pop(); |
| 40 | + indexesAtname[popElement[0]][1] = i; |
| 41 | + } |
| 42 | + } |
| 43 | + name = 0; |
| 44 | + for(int i = 0; i < arr.length(); i++){ |
| 45 | + char c = cs[i]; |
| 46 | + if(i == x){ |
| 47 | + for(int[] element: stack){ |
| 48 | + element[1] = 1; |
| 49 | + } |
| 50 | + }else if(i == y){ |
| 51 | + isFindMode = true; |
| 52 | + } |
| 53 | + if(c == '0'){ |
| 54 | + // 넣기 |
| 55 | + indexesAtname[name][0] = i; |
| 56 | + stack.push(new int[]{name++, 0}); |
| 57 | + if(i == x){ |
| 58 | + stack.peek()[1] = 1; |
| 59 | + } |
| 60 | + }else{ |
| 61 | + // 빼기 |
| 62 | + popElement = stack.pop(); |
| 63 | + indexesAtname[popElement[0]][1] = i; |
| 64 | + if(isFindMode && popElement[1] == 1) |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + StringBuilder sb = new StringBuilder(); |
| 70 | + sb.append(indexesAtname[popElement[0]][0] + 1); |
| 71 | + sb.append(" "); |
| 72 | + sb.append(indexesAtname[popElement[0]][1] + 1); |
| 73 | + System.out.println(sb.toString()); |
| 74 | + br.close(); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
0 commit comments