알고리즘/프로그래머스
프로그래머스 - 2018 KAKAO BLIND RECRUITMENT [3차] 방금그곡
시나모온
2020. 5. 5. 16:39
문제 링크입니다 : https://programmers.co.kr/learn/courses/30/lessons/17683
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
처음에는 착각해서 #이 들어간 데이터를 2분짜리 멜로디로 착각했다.
그래서 한참 고생하다가, 데이터를 파싱하기 전에 전처리를 해야겠다고 생각했다.
C# -> c, B# -> b, F# -> f, G# -> g, A# -> a 이렇게 전처리를 하고 나니 다른 부분은 구현문제라 크게 어렵지는 않았다.
전처리의 중요성을 정말 저~~~~~ㅇ말 많이 깨달은 문제이다.
그런데 이 문자 내가 이해를 잘 못 한건지 좀 이해가 안되는 부분이 있다.
예를 들어, 첫번재 곡이 끝나고 바로 두번째곡이 나와서 첫번째랑 두번째랑 겹쳐서 만들어진 멜로디 안에서도 m을 찾을 수 있지 않을까 생각했었다. 이런 거를 고려하면 문제가 너무 어렵다고 생각해서 제외하고 풀었는데... 각 곡들마다 구분이 된다는 명확한 조건을 주지 않았다는 점에서 좀 아쉬움이 남는 문제다.
// https://programmers.co.kr/learn/courses/30/lessons/17683
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Result {
int pTime;
int index;
string title;
Result(int a, int b, string c) : pTime(a), index(b), title(c) {}
};
bool compare(const Result& a, const Result& b) {
if(a.pTime == b.pTime) {
return a.index < b.index;
}
return a.pTime > b.pTime;
}
string solution(string m, vector<string> musicinfos) {
string answer = "";
vector<Result> ret;
// C# -> c, B# -> b, F# -> f, G# -> g, A# -> a으로 변환
while(true) {
int idx = m.find('#');
if(idx == -1) break;
string temp = "";
temp += m[idx - 1] - 'A' + 'a';
m.replace(idx - 1, 2, temp);
}
for(int i = 0; i < musicinfos.size(); i++) {
// 곡정보 뽑아내기
string str = musicinfos[i];
int sTime = stoi(str.substr(0, 2)) * 60 + stoi(str.substr(3, 2));
int eTime = stoi(str.substr(6, 2)) * 60 + stoi(str.substr(9, 2));
int pTime = eTime - sTime;
int commaIdx = str.find(',', 12);
string title = str.substr(12, commaIdx - 12);
string melody = str.substr(commaIdx + 1, str.size() - commaIdx);
// C# -> c, B# -> b, F# -> f, G# -> g, A# -> a으로 변환
while(true) {
int idx = melody.find('#');
if(idx == -1) break;
string temp = "";
temp += melody[idx - 1] - 'A' + 'a';
melody.replace(idx - 1, 2, temp);
}
// 해당 곡이 재생된 시간동안 생성된 멜로디 만들기
int melTime = melody.size();
string fullMelody = "";
int cnt = pTime / melTime;
while(cnt-- != 0) {
fullMelody += melody;
}
fullMelody += melody.substr(0, (pTime % melTime));
// m과 전체 재생된 멜로디에서 일치하는 부분 있는지 찾고
// 일치하는 부분 있다면 그 부분 저장
int findIdx = 0;
findIdx = fullMelody.find(m, findIdx);
if(findIdx != -1) {
ret.push_back({pTime, i, title});
}
}
// 못 찾았는지 확인 후, 찾았으면 정렬하기
if(ret.empty()) {
answer = "(None)";
} else {
sort(ret.begin(), ret.end(), compare);
answer = ret[0].title;
}
return answer;
}
개발 환경 : vscode
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~