[Programmers] Lv2. [3์ฐจ] ํ์ผ๋ช
์ ๋ ฌ | C++
2023. 6. 24. 16:36ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
- File ํด๋์ค๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
- File ํด๋์ค์ id(int), originalName(string), head(string), number(string), tail(string) ๋ฉค๋ฒ ๋ณ์๊ฐ ์์ต๋๋ค.
- id ๊ฐ์ ๋ฌธ์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ files ๋ฐฐ์ด์ ์ธ๋ฑ์ค ์์์ ๋๋ค. head์ number๊ฐ ๊ฐ์ ๋, ์ ๋ ฌ ๊ธฐ์ค์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
- ์์ฑ์์์ ์๋ณธ ๋ฌธ์์ด์ head, number, tail๋ก ์ ๋ถ๋ฆฌํ์ฌ ์ ์ฅํ๋๋ก ํฉ๋๋ค.
- < ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ ํด์ค๋๋ค.
- BST ์๋ฃ๊ตฌ์กฐ์ธ set์ File ๋ชฉ๋ก๋ค์ ์ ์ฅํด ๋ ์์ ์ ๋๋ค.
- ์ปค์คํ
ํด๋์ค๋ฅผ set์ ์ ์ฅํ๋ ค๋ฉด < ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ด ํ์ํฉ๋๋ค.
- ๋ฌธ์ ์์ ์ ์๋ ๊ธฐ์ค์ผ๋ก ์์ฑํด์ค๋๋ค.
- File ํด๋์ค์ id(int), originalName(string), head(string), number(string), tail(string) ๋ฉค๋ฒ ๋ณ์๊ฐ ์์ต๋๋ค.
- files ๋ฐฐ์ด์ ์ํํ๋ฉด์ set<File>์ ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํด์ค๋๋ค.
- set<File>์ ์ํํ๋ฉด์ answer ๋ฐฐ์ด์ ์ถ๊ฐํ ํ, ๋ฆฌํดํฉ๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
#include <set>
using namespace std;
class File {
public:
File(int id, const string& fileName) {
_id = id;
_originalName = fileName;
Init();
}
string ToLower(const string& str) {
string result(str);
for (int i = 0; i < result.length(); i++)
result[i] = tolower(result[i]);
return result;
}
bool operator<(const File& other) const {
int dictionaryOrder = _head.compare(other._head);
if (dictionaryOrder == 0) {
if (_number.empty() or other._number.empty())
return _id < other._id;
int myNum = stoi(_number);
int otherNum = stoi(other._number);
return myNum != otherNum ? myNum < otherNum : _id < other._id;
}
return dictionaryOrder < 0;
}
private:
void Init() {
int begin = 0, i = 0, end = _originalName.length();
/* HEAD ์ฒ๋ฆฌ */
for (; i < end; i++) {
if (isdigit(_originalName[i])) {
_head = ToLower(_originalName.substr(begin, i));
begin = i;
break;
}
}
if (i == end) {
_head = ToLower(_originalName.substr(begin, i));
return;
}
/* NUMBER ์ฒ๋ฆฌ */
for (; i < end; i++) {
if (!isdigit(_originalName[i])) {
_number = _originalName.substr(begin, i - begin);
begin = i;
break;
}
}
if (i == end) {
_number = _originalName.substr(begin, i - begin);
return;
}
/* TAIL ์ฒ๋ฆฌ */
_tail = _originalName.substr(begin);
}
public:
int _id;
string _originalName;
string _head;
string _number;
string _tail;
};
vector<string> solution(vector<string> files) {
vector<string> answer;
set<File> orderedFiles;
for (int i = 0; i < files.size(); i++)
orderedFiles.emplace(File(i, files[i]));
for (const auto file : orderedFiles)
answer.emplace_back(file._originalName);
return answer;
}
728x90
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ | C++ (0) | 2023.06.25 |
---|---|
[Programmers] Lv2. [1์ฐจ] ํ๋ ์ฆ4๋ธ๋ก | C++ (0) | 2023.06.24 |
[Programmers] Lv2. ๋ฐฉ๋ฌธ ๊ธธ์ด | C++ (0) | 2023.06.24 |
[Programmers] Lv2. ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ | C++ (0) | 2023.06.22 |
[Programmers] Lv2. ์ฃผ์๊ฐ๊ฒฉ | C++ (0) | 2023.06.22 |