|
| 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 | + |
| 9 | + static HashMap<Character,Integer> boomWord = new HashMap<>(); |
| 10 | + static StringBuilder sb = new StringBuilder(); |
| 11 | + static Char[] chars; |
| 12 | + |
| 13 | + static class Char{ |
| 14 | + |
| 15 | + Char pre = null; |
| 16 | + Char post = null; |
| 17 | + char ch; |
| 18 | + boolean isAlive = true; |
| 19 | + |
| 20 | + public Char(char ch){ |
| 21 | + this.ch = ch; |
| 22 | + } |
| 23 | + |
| 24 | + public boolean checkBoom(int idx){ |
| 25 | + |
| 26 | + if (boomWord.containsKey(ch) && boomWord.get(ch) == idx) { |
| 27 | + if (idx == boomWord.size()-1) return true; |
| 28 | + if (post== null) return false; |
| 29 | + |
| 30 | + return post.checkBoom(idx+1); |
| 31 | + } |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + public Char boom(){ |
| 36 | + return processBoom(pre,0); |
| 37 | + } |
| 38 | + private Char processBoom(Char pre, int idx){ |
| 39 | + if (idx == boomWord.size()){ |
| 40 | + |
| 41 | + if (this.pre != null && pre != null) { |
| 42 | + this.pre = pre; |
| 43 | + pre.post = this; |
| 44 | + } |
| 45 | + |
| 46 | + return this; |
| 47 | + } |
| 48 | + isAlive = false; |
| 49 | + |
| 50 | + if ( idx == boomWord.size()-1 && this.post == null) { |
| 51 | + if (pre != null )pre.post = null; |
| 52 | + return pre; |
| 53 | + } |
| 54 | + return post.processBoom(pre,idx+1); |
| 55 | + } |
| 56 | + |
| 57 | + public Char doReposition(){ |
| 58 | + // 폭발단어에 포함안되면 굳이 이동 필요 x; |
| 59 | + if (!boomWord.containsKey(ch)) return this; |
| 60 | + |
| 61 | + int curIdx = boomWord.get(ch); |
| 62 | + return moveToFirst(curIdx); |
| 63 | + } |
| 64 | + |
| 65 | + private Char moveToFirst(int idx){ |
| 66 | + if (idx == 0 || pre == null ) return this; |
| 67 | + if (!boomWord.containsKey(ch) || boomWord.get(ch) != idx) return this; |
| 68 | + |
| 69 | + return pre.moveToFirst(idx-1); |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + public static void main(String[] args) throws IOException { |
| 77 | + init(); |
| 78 | + process(); |
| 79 | + print(); |
| 80 | + } |
| 81 | + |
| 82 | + private static void init() throws IOException { |
| 83 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 84 | + String input = br.readLine(); |
| 85 | + |
| 86 | + chars = new Char[input.length()]; |
| 87 | + chars[0] = new Char(input.charAt(0)); |
| 88 | + for (int i = 1; i < chars.length; i++) { |
| 89 | + chars[i] = new Char(input.charAt(i)); |
| 90 | + chars[i].pre = chars[i-1]; |
| 91 | + chars[i-1].post = chars[i]; |
| 92 | + } |
| 93 | + |
| 94 | + String target = br.readLine(); |
| 95 | + |
| 96 | + for (int i = 0; i < target.length(); i++) { |
| 97 | + boomWord.put(target.charAt(i), i); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private static void process() { |
| 103 | + |
| 104 | + Char cur = chars[0]; |
| 105 | + |
| 106 | + while (cur != null) { |
| 107 | + if(cur.checkBoom(0)){ |
| 108 | + cur = cur.boom(); |
| 109 | + if (cur != null) cur = cur.doReposition(); |
| 110 | + } else{ |
| 111 | + cur = cur.post; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + for (int i = 0; i < chars.length; i++) { |
| 117 | + if (chars[i].isAlive) sb.append(chars[i].ch); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + private static void print() { |
| 128 | + System.out.println(sb.length()==0 ? "FRULA" : sb.toString()); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
0 commit comments