Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
29 30
Archives
Today
Total
관리 메뉴

Life Engineering

[프로그래머스] 방금그곡 (C++) 본문

Problem Solving

[프로그래머스] 방금그곡 (C++)

흑개 2021. 11. 28. 00:13

https://programmers.co.kr/learn/courses/30/lessons/17683

 

코딩테스트 연습 - [3차] 방금그곡

방금그곡 라디오를 자주 듣는 네오는 라디오에서 방금 나왔던 음악이 무슨 음악인지 궁금해질 때가 많다. 그럴 때 네오는 다음 포털의 '방금그곡' 서비스를 이용하곤 한다. 방금그곡에서는 TV,

programmers.co.kr

#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보다 길 경우 교체해주는 방식으로 하면 된다.)