일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 일상회피
- javap
- IMPLEMENT
- 회피
- 여행
- abstract
- Shared Elements
- Interface
- Navigation Component
- Android
- bytecode
- extends
- 심리여행
- opcode
- 보안취약점
- jvm
- 취약점
- 보안
- 일상탈출
- 심리학
- 치유
- bytecode 분석
- 버킷리스트
- Transition
- HelloWorld
- 여행계획
- Recylcer
- static
- throws
- ㅇ
Archives
- Today
- Total
패스트터틀
(Baekjoon) GreedyAlgorithm - (27) 크게 만들기 본문
2812번: 크게 만들기
문제 N자리 숫자가 주어졌을 때, 여기서 숫자 K개를 지워서 얻을 수 있는 가장 큰 수를 구하는 프로그램을 작성하시오. 입력 첫째 줄에 N과 K가 주어진다. (1 ≤ K < N ≤ 500,000) 둘째 줄에 N자리 숫자가 주어진다. 이 수는 0으로 시작하지 않는다. 출력 입력으로 주어진 숫자에서 K개를 지웠을 때 얻을 수 있는 가장 큰 수를 출력한다. 예제 입력 1 복사 4 2 1924 예제 출력 1 복사 94...
www.acmicpc.net
쉬운것 같으면서도 복잡해보이는 문제이다.
푸는법은 깨달았으나 머라고 해야할까 하나하나 따지는게 복잡한 문제다. 스택을 사용하여도 되고 사용하지 않아도 되지만 스택 라이브러리를 끌어다쓸경우 값을 다시 꺼내와서 뒤집어야 한다. 그러므로 그냥 top 변수 하나두고 스택처럼 만들고 출력하는것이 편하다.
package GreedyAlgorithm;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class _2812 {
public static void main(String args[]) throws IOException {
BufferedReader Input_Buffer = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter Output_Buffer = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st_token = new StringTokenizer(Input_Buffer.readLine(), " ");
int Number_length = Integer.parseInt(st_token.nextToken());
int Delete_num = Integer.parseInt(st_token.nextToken());
String digit = Input_Buffer.readLine();
char stack_digit[] = new char[Number_length - Delete_num];
int stack_top = 0;
for (int i = 0; i < Number_length; i++) {
if(stack_top == Number_length - Delete_num) break;
while (stack_top > 0 && Delete_num > 0 && stack_digit[stack_top - 1] < digit.charAt(i)) {
stack_top--;
Delete_num--;
}
stack_digit[stack_top++] = digit.charAt(i);
}
for (int i = 0; i < stack_digit.length ; i++)
Output_Buffer.write(stack_digit[i]);
Output_Buffer.flush();
Input_Buffer.close();
Output_Buffer.close();
}
}
백준문제풀이Github :
https://github.com/sdk0213/baekjoon-study
sdk0213/baekjoon-study
solve a baekjoon question. Contribute to sdk0213/baekjoon-study development by creating an account on GitHub.
github.com
'Algorithm > baekjoon' 카테고리의 다른 글
(Baekjoon) GreedyAlgorithm - (26) 택배 (0) | 2020.02.17 |
---|---|
(Baekjoon) GreedyAlgorithm - (25) 책 나눠주기 (0) | 2020.02.07 |
(Baekjoon) GreedyAlgorithm - (24) 빵집 (0) | 2020.02.04 |
(Baekjoon) GreedyAlgorithm - (23) 보석 도둑 (0) | 2020.01.28 |
(Baekjoon) GreedyAlgorithm - (22) 문서 검색 (0) | 2020.01.16 |
Comments