728x90
반응형
https://www.acmicpc.net/problem/2812
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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class number_2812 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
String number = br.readLine();
StringBuilder sb = new StringBuilder();
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < number.length(); i++) {
int num = number.charAt(i) - '0';
if (stack.isEmpty()) {
stack.push(num);
continue;
}
while (!stack.isEmpty() && stack.peek() < num) {
if (stack.size() + number.length() - (i + 1) < N - K) {
break;
}
stack.pop();
}
stack.push(num);
}
while (stack.size() != N - K) {
stack.pop();
}
for (int value : stack) {
sb.append(value);
}
System.out.println(sb.toString());
}
}
|
cs |
728x90
'Algorithm > BOJ' 카테고리의 다른 글
백준) 3055_탈출 (2) | 2023.05.19 |
---|---|
백준) 1449_수리공 항승 (0) | 2023.05.18 |
백준) 7569_토마토2 (2) | 2023.05.17 |
백준) 7576_토마토 (1) | 2023.05.17 |
백준) 2133_타일 채우기 (0) | 2023.05.16 |