[프로그래머스] 파일명 정렬
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp(string s1, string s2) {
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
string h1 = "";
string num1 = "";
bool headOver = false;
for (int i = 0; i < s1.length(); ++i) {
char ch = s1[i];
if (ch >= '0' && ch <= '9') {
headOver = true;
num1 += ch;
}
else if (!headOver) {
h1 += ch;
continue;
}
else break;
}
string h2 = "";
string num2 = "";
headOver = false;
for (int i = 0; i < s2.length(); ++i) {
char ch = s2[i];
if (ch >= '0' && ch <= '9') {
headOver = true;
num2 += ch;
}
else if (!headOver) {
h2 += ch;
continue;
}
else break;
}
if (h1 < h2) return true;
if (h1 == h2) {
return stoi(num1) < stoi(num2);
}
return false;
}
vector<string> solution(vector<string> files) {
stable_sort(files.begin(), files.end(), cmp);
vector<string> answer(files.begin(), files.end());
return answer;
}