C++:: 프로그래머스 < [3차] 파일명 정렬 >

jahlee·2023년 7월 11일
0

프로그래머스_Lv.2

목록 보기
75/106
post-thumbnail

주어지는 조건대로 head, number, tail로 나누어서 정렬을 해주면 되는 문제이다. 이 문제의 특이한 점은 head와 number가 같은 경우 들어온 입력 순으로 뱉어내주어야 한다는 점인데, 이때문에 sort대신 statble_sort를 사용해야한다.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> sepString(const string& s) {
    vector<string> res;

    int head = 0;
    while (!isdigit(s[head])) head++;// 첫 숫자 찾을때까지 밀어준다.
    res.push_back(s.substr(0, head));// 숫자 전까지가 head
    res.push_back(s.substr(head, 5));// 숫자부터 최대 5개가 number가 될수있는 범위
    // tail은 사실상 생각하지 않아도 되는 부분이다.
    for (int i=0; i<head; i++) {// 대소문자 동일하게 내려준다.
        res[0][i] = tolower(res[0][i]);
    }
    return res;
}

bool compare(const string& a, const string& b) {
    vector<string> a_part, b_part;
    a_part = sepString(a);
    b_part = sepString(b);
    if (a_part[0] != b_part[0]) {// head 사전순 정렬
        return a_part[0] < b_part[0];
    } 
    return stoi(a_part[1]) < stoi(b_part[1]);// 정수로 바꿨을때 오름차순, 동일하다면 들어온 순서대로 리턴
}

vector<string> solution(vector<string> files) {
    stable_sort(files.begin(), files.end(), compare);
    return files;
}

0개의 댓글