Life Engineering
[프로그래머스] 방금그곡 (C++) 본문
https://programmers.co.kr/learn/courses/30/lessons/17683
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
struct info{
string name;
int time;
int order;
};
bool cmp(info i1, info i2){
if (i1.time==i2.time){
return i1.order < i2.order;
}
return i1.time > i2.time;
}
int convertTime(string s){
stringstream ss(s);
string temp;
vector<string> result;
while (getline(ss, temp, ':')){
result.push_back(temp);
}
return (stoi(result[0])*60)+stoi(result[1]);
}
string solution(string m, vector<string> musicinfos) {
string answer = "";
vector<info> ans;
for (int i=0; i<musicinfos.size(); i++){
int start, end, time;
string music="";
vector<string> temp;
vector<string> notes;
stringstream ss(musicinfos[i]);
string s;
while (getline(ss, s, ',')){
temp.push_back(s);
}
for (int j=0; j<temp[3].size(); j++){
string note(1, temp[3][j]);
if (j!=temp[3].size()-1){
if (temp[3][j+1]=='#'){
j++;
note.push_back('#');
}
}
notes.push_back(note);
}
start=convertTime(temp[0]);
end=convertTime(temp[1]);
time=end-start;
int notesSize=notes.size();
//time 만큼 음표 늘려주기
for (int t=0; t<time; t++){
music+=notes[t%notesSize];
}
//찾기
size_t pos=0;
while ((pos=music.find(m, pos))!=string::npos){ //연속적으로 찾는다
if (pos+m.size()==music.size() || music[pos+m.size()]!='#'){
ans.push_back({temp[2], time, i});
break;
}
pos+=m.size();
}
}
if (ans.size()==0)
answer="(None)";
else{
sort(ans.begin(), ans.end(), cmp);
answer=ans[0].name;
}
return answer;
}
#이 붙은 음과 그렇지 않은 음을 구분하기 위해 많은 노력을 들였으나, 사실 # 붙은 음은 다른 문자로 처리해줘서 비교를 더 쉽게 할 수 있었다. "치환" 하면 문제가 더 쉬워진다.
시간 계산할 때 stringstream을 이용해 parsing해주었지만, substr을 이용하면 더 간단해진다.
음표를 늘려줄 때, 원래 악보를 notes에 저장해서 그것을 % 연산 후 string인 music에 append 해주는 방식으로 늘려주었다.
#를 구분하느라 상당히 지저분해졌지만, 다른 분들의 코드를 보니 아예 다른 문자로 변환해주면 더 간단해진다.
늘린 악보를 find로 비교하고, npos 가 나타나지 않으면 answer을 갱신해주면 된다.
나처럼 따로 ans 배열을 만들어서 조건에 맞는 애들이 제일 앞에 위치하도록 하지 않아도 된다.
pair<string, int> ans 를 따로 만들어서 만약 조건에 맞는 애들이 있을 경우 시간만 비교해줘서 갱신해주면 된다.
(time이 기존 ans에 있는 time보다 길 경우 교체해주는 방식으로 하면 된다.)
'Problem Solving' 카테고리의 다른 글
[BOJ 17142] 연구소 3 (C++) (0) | 2021.12.15 |
---|---|
[프로그래머스] 피로도 (C++) (0) | 2021.12.06 |
[프로그래머스] 캐시 (C++) (0) | 2021.11.27 |
[프로그래머스] 주식 가격 (C++) (0) | 2021.11.19 |
[프로그래머스] 이진 변환 반복하기 (C++) (0) | 2021.11.18 |