-
Notifications
You must be signed in to change notification settings - Fork 6
/
dongwoo.java
35 lines (34 loc) · 990 Bytes
/
dongwoo.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.StringTokenizer;
public class dongwoo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
Deque<Character> deq = new ArrayDeque<>();
String text = br.readLine();
int k = 0;
for (int i = 0; i < N; i++) {
char c = text.charAt(i);
if (deq.isEmpty() || deq.peekLast() >= c || k >= K) {
deq.addLast(c);
} else {
while (!deq.isEmpty() && deq.peekLast() < c && k < K) {
deq.pollLast();
k++;
}
deq.addLast(c);
}
}
for (int i=0; i<N-K; i++) {
sb.append(deq.pollFirst());
}
System.out.println(sb);
}
}