반응형
2812번: 크게 만들기
N자리 숫자가 주어졌을 때, 여기서 숫자 K개를 지워서 얻을 수 있는 가장 큰 수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
받은 입력 문자열을 하나씩 스택에 넣고
스택의 top위치 값이 현재 값보다 크면 최대 K번 pop한다
그럼 내림차순 형태의 수가 나오게 된다.
모두 같은 숫자일 경우에는 더이상 지워지지 않으므로
마지막에 N-K길이의 문자열로 출력하도록 했습니다
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
using namespace std;
int main() {
int N, K;
cin>>N>>K;
string s;
cin>>s;
stack<int> st;
int d = 0;
st.push(s[0]-'0');
int limit = 0;
for(int i = 1; i<N; i++){
while(limit<K){
if(!st.empty()&&st.top()<s[i]-'0'){
d = st.top();
st.pop();
limit++;
}
else{
break;
}
}
d = s[i]-'0';
st.push(s[i]-'0');
d = st.size();
}
string ans = "";
while(!st.empty()){
ans += st.top() + '0';
st.pop();
}
reverse(ans.begin(), ans.end());
for(int i = 0; i<N-K; i++){
cout<<ans[i];
}
}
반응형
'🕹️ 알고리즘 > 💯 코딩테스트' 카테고리의 다른 글
[프로그래머스-level1]완주하지 못한 선수 (0) | 2021.02.14 |
---|---|
[3079]입국심사 (0) | 2021.02.07 |
[프로그래머스-level1]모의고사 (0) | 2021.01.08 |
[프로그래머스-level1][1차]다트게임 (0) | 2021.01.08 |
[백준-16693]Pizza Deal(C++) (0) | 2020.11.27 |