-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241113-9935.java
53 lines (50 loc) · 1.65 KB
/
241113-9935.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().toCharArray();
char[] bomb = br.readLine().toCharArray();
if (bomb.length == 0) {
System.out.print(str);
return;
}
int bi = bomb.length - 1;
Deque<Character> dq = new ArrayDeque<>();
for (int i = 0; i < str.length; i++) {
dq.addLast(str[i]);
if (dq.peekLast() == bomb[bi]) {
Stack<Character> popped = new Stack<>();
for (int j = bi; j >= 0; j--) {
if (dq.isEmpty()) {
while (!popped.isEmpty()) {
dq.addLast(popped.pop());
}
break;
}
char pop = dq.pollLast();
popped.add(pop);
if (pop != bomb[j]) {
while (!popped.isEmpty()) {
dq.addLast(popped.pop());
}
break;
}
}
}
}
if (dq.isEmpty()) {
System.out.print("FRULA");
return;
}
StringBuilder sb = new StringBuilder();
while (!dq.isEmpty()) {
sb.append(dq.pollFirst());
}
System.out.print(sb);
}
}