Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- BeautifulSoup
- 큐
- 크루스칼
- lis
- 피보나치 수
- 순열
- 빠른 입출력
- 스택
- Knapsack
- dfs
- 그리디
- DP
- 재귀
- 링크드리스트
- 세그먼트 트리
- 문자열
- 조합
- 이분 탐색
- 우선순위 큐
- SSAFY
- 분할 정복
- 시뮬레이션
- 완전 탐색
- 클래스
- 중복 순열
- BFS
- 백트래킹
- 비트마스크
- MST
- 메모리풀
Archives
- Today
- Total
작심 24/7
[프로그래머스] 파일명 정렬 2018 KAKAO BLIND RECRUITMENT 본문
level 2 길래 가벼운 마음으로 시작했다가 무거운 마음으로 겨우 끝냈다.
문제의 조건을 꼼꼼히 읽어봤으면 더 빨리 끝낼 수 있었을까..?
1. HEAD를 비교할 때 소문자로 바꾸어 준다
2. NUMBER는 최대 5개의 숫자로만 이루어져 있다
3. NUMBER의 앞부분에 0이 있으면 일단 size를 체크해 뒀다가 나중에 출력 시 앞에 더해준다
4. HEAD, NUMBER, TAIL의 구분을 명확히 해야 한다
ex) aBc009dd78 -> HEAD=aBc, NUMBER=009, TAIL=dd78
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Standard {
public:
string head, tail;
int number, index, number_size;
Standard(string head, int number, int index, string tail, int number_size) {
this->head = head;
this->number = number;
this->index = index;
this->tail = tail;
this->number_size = number_size;
}
};
bool compare(Standard a, Standard b) {
string A = a.head, B = b.head;
std::transform(A.begin(), A.end(), A.begin(), ::tolower); //소문자로 바꿔주는 함수
std::transform(B.begin(), B.end(), B.begin(), ::tolower);
if (A == B) { //HEAD가 같을 경우 NUM을 기준으로 정렬
if (a.number == b.number) return a.index < b.index; //NUM도 같을 경우 index 기준으로 정렬
else return a.number < b.number;
}
else return A < B;
}
vector<string> solution(vector<string> files) {
vector<string> answer;
vector<Standard> v;
for (int i = 0; i < files.size(); i++) {
string HEAD, NUM, TAIL;
int NUMBER = 0, HEAD_cnt = 0, NUMBER_cnt = 0, TAIL_cnt = 0;
for (int j = 0; j < files[i].size(); j++) {
if (NUMBER_cnt == 0 && (files[i][j] == '.' || files[i][j] == '-' || files[i][j] == ' '
|| (files[i][j] >= 'a'&&files[i][j] <= 'z') || (files[i][j] >= 'A'&&files[i][j] <= 'Z'))) { //숫자가 나오기 전까진 HEAD
HEAD += files[i][j];
HEAD_cnt++;
}
else if (TAIL_cnt == 0 && NUMBER_cnt < 5 && HEAD_cnt != 0 && files[i][j] >= 48 && files[i][j] <= 57) { //숫자가 처음 나올 때부터 NUMBER. 최대 5개까지의 숫자만 취급한다
NUM += files[i][j];
NUMBER_cnt++;
}
else if (HEAD_cnt != 0 && NUMBER_cnt != 0) { //이후는 무조건 TAIL
TAIL += files[i][j];
TAIL_cnt++;
}
}
int ten = 1;
for (int n = NUMBER_cnt - 1; n >= 0; n--) { //string형 NUM를 int형 NUMBER로 바꿔줌
NUMBER += ten*(NUM[n] - 48);
ten *= 10;
}
v.push_back(Standard(HEAD, NUMBER, i, TAIL, NUMBER_cnt));
}
sort(v.begin(), v.end(), compare);
for (int i = 0; i < v.size(); i++) {
string Number, temp;
temp = to_string(v[i].number);
if (temp.size() < v[i].number_size) { //빈 칸이 있으면 그만큼 앞에서부터 0으로 채워준다
for (int j = 0; j < v[i].number_size - temp.size(); j++) {
Number += '0';
}
}
Number += temp;
answer.push_back(v[i].head + Number + v[i].tail);
}
return answer;
}
'프로그래머스 > Level 2' 카테고리의 다른 글
[프로그래머스] 뉴스 클러스터링 2018 KAKAO BLIND RECRUITMENT (0) | 2020.05.26 |
---|---|
[프로그래머스] 멀쩡한 사각형 Summer/Winter Coding(2019) (0) | 2020.05.20 |
Comments