구현
File 이라는 구조체를 형성한다. 구조체의 내용물은 다음과 같다.
기존의 문자열을 head와 number로 나눈 후 File 구조체를 생성한다.
File 구조체를 기반으로 정렬을 시도한다.
정렬된 File 구조체 순서를 기반으로, 본래의 문자열들을 정답에 대입한다.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector<string> go(string str) {
int idx = 0;
string tmp = "";
string num = "";
vector<string> s;
while(idx != str.size()) {
if('0'<=str[idx] && str[idx]<='9') {
num+=str[idx];
} else {
if(num!="") break;
tmp+=tolower(str[idx]);
}
idx++;
}
s.push_back(tmp);
s.push_back(to_string(stoi(num)));
return s;
}
struct File {
int i;
string h;
int n;
string s;
};
bool cmp(File a, File b) {
if(a.h == b.h && a.n == b.n) return a.i < b.i;
else if(a.h == b.h) return a.n < b.n;
return a.h < b.h;
}
vector<string> solution(vector<string> files) {
vector<File> tmp;
vector<string> answer;
for(int i=0; i<files.size(); i++) {
vector<string> s = go(files[i]);
File f;
f.i = i;
f.h = s[0];
f.n = stoi(s[1]);
f.s = files[i];
tmp.push_back(f);
}
sort(tmp.begin(), tmp.end(), cmp);
for(File a : tmp) {
answer.push_back(a.s);
}
return answer;
}