Skip to content

Commit f1ed5e3

Browse files
committed
[PGS] [3차] 17686 파일명 정렬 (Lv.2)
1 parent 5c9ef18 commit f1ed5e3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

박예진/3주차/260114.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//https://school.programmers.co.kr/learn/courses/30/lessons/17686
2+
3+
#include <string>
4+
#include <vector>
5+
#include <iostream>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
struct File {
11+
int idx;
12+
string head;
13+
int number;
14+
};
15+
16+
struct cmp {
17+
bool operator()(const File &f1, const File &f2) {
18+
if (f1.head != f2.head) return f1.head < f2.head;
19+
else if (f1.number != f2.number) return f1.number < f2.number;
20+
else return f1.idx < f2.idx;
21+
}
22+
};
23+
24+
vector<File> v;
25+
26+
vector<string> solution(vector<string> files) {
27+
vector<string> answer;
28+
29+
for(int i = 0; i < files.size(); i++) {
30+
string head = "";
31+
int idx = 0;
32+
for(int j = 0; j < files[i].size(); j++){
33+
if ('0' <= files[i][j] && files[i][j] <= '9') {
34+
idx = j;
35+
break;
36+
}
37+
head += tolower(files[i][j]);
38+
}
39+
40+
string number = "";
41+
for(int j = idx; j < files.size(); j++){
42+
if (!('0' <= files[i][j] && files[i][j] <= '9')) {
43+
idx = j;
44+
break;
45+
}
46+
number += files[i][j];
47+
}
48+
49+
File f;
50+
f.idx = i;
51+
f.head = head;
52+
f.number = stoi(number);
53+
54+
v.push_back(f);
55+
}
56+
57+
sort(v.begin(), v.end(), cmp());
58+
59+
for(int i = 0; i < v.size(); i++){
60+
answer.push_back(files[v[i].idx]);
61+
}
62+
63+
return answer;
64+
}

0 commit comments

Comments
 (0)