본문 바로가기

PS/boj

백준 16719 ZOAC

http://www.acmicpc.net/problem/16719 

 

16719번: ZOAC

2018년 12월, 처음 시작하게 된 ZOAC의 오프닝을 맡은 성우는 누구보다 화려하게 ZOAC를 알리려 한다. 앞 글자부터 하나씩 보여주는 방식은 너무 식상하다고 생각한 성우는 문자열을 보여주는 새로

www.acmicpc.net

우선순위 큐를 사용해 푸는 문제였습니다.

현재까지의 ZOAC 문자열을 bool 배열을 통해 표현한 후 추가할 문자열에 대해 사전순으로 앞서는지 확인하기 위해

우선순위큐에 현재 문자를 추가했을때 문자열과 추가한 문자의 index를 pair로 추가했습니다. 문자열에 대해 삽입연산은 시간이 오래걸리므로 bool 배열을 통해 추가해줬습니다.

#include <stdio.h>
#include<queue>
#include<iostream>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
void fast_io() {
  cin.tie(0)->sync_with_stdio(0);
}
bool chk[101];
int main(){
    fast_io();
    string input;
    cin>>input;
    int k=(int)input.size();
    for(int x=0;x<k;x++){
        priority_queue<pair<string,int>,vector<pair<string,int>>,greater<pair<string,int>>> pq;
        for(int y=0;y<k;y++){
            if(chk[y]) continue;
            string now;
            for(int z=0;z<k;z++){
                if(chk[z] || y==z) now.push_back(input[z]);
            }
            pq.push({now,y});
        }
        chk[pq.top().second]=true;
        for(int y=0;y<k;y++){
            if(chk[y]) cout<<input[y];
        }
        cout<<'\n';
    }
}

'PS > boj' 카테고리의 다른 글

백준 6987 월드컵  (0) 2023.01.02
백준 16965 구간과 쿼리  (0) 2023.01.01
백준 2026 소풍  (0) 2022.09.01
백준 20946 합성인수분해  (0) 2022.08.31
백준 1016 제곱 ㄴㄴ 수  (0) 2022.08.22