프로그래머스/Level_2

[프로그래머스 Level_2 / C++ / 카카오] 방금그곡

배발자 2022. 2. 17.

목차

    반응형

    [프로그래머스 Level_2 / C++ / 카카오] 방금그곡

    문제를 봤을 때 역시나 카카오는 문자열을 예쁘게(?) 준다. 

     

    #접근방법

     

    (1)

     먼저 m 의 문자열은 12개의 문자열을 나타낸다. 

    여기서 중요한 점은 'C#' 또한 하나의 음이라는 것이다. 그렇기 때문에 본인은 '#'을 우측에 두고 있는 알파벳에다가 +10 정도를 해주면서 다른 알파벳으로 나타냈다. 

    즉, 'C' 는 아스키코드로 67 이기 때문에 +10 을 해주면 'M'으로 치환된다. 

     

    "CC#BCC#BCC#BCC#BCC#B" -> "CMBCMBCMBCMBCMBCMB" 

     

    (2) 

     

    <substr> 인덱스로 문자열 자르기

    문자열을 처리하기 위해서 정말 많이 쓰이는 함수이다. 이 함수는 어떠한 문자열의 특정 인덱스에서 어떠한 인덱스까지 사이의 새로운 문자열을 만들때 보통 사용된다. 예를 들어, "abcdefe" 라는...

    baebalja.tistory.com

    musicinfos의 각 문자열을 잘라야 하는데 쉼표를 찾을때 마다 잘라야한다. 

    문자열 자르는 것이 어렵다면 위의 글을 한번 읽어보는 것을 추천한다. 

     

    문자열을 잘라서 시작 시간과 끝나는 시간을 저장하는데

    이때, 시 단위를 분 단위 변환한다. 

     

    ex) 01:15 ~ 02:05  ->   75 ~ 125  (재생시간 50분) 

     

    (3)

    musicinfos 안에 있는 악보 내용 또한 1번과 같이 수정한다. (# 없애기) 

     

    (4) 

    M의 값을 갖고 있는 음악이면서 가장 긴 재생시간을 갖고 있다면 answer 변수에 음악 제목을 저장한다.  

    
      
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    string solution(string m, vector<string> musicinfos) {
    string answer = "";
    int max_time =-1;
    int pos;
    while((pos = m.find('#'))!=string::npos){
    m[pos-1]=m[pos-1]+10;
    m = m.substr(0,pos)+m.substr(pos+1);
    }
    for(int i=0; i<musicinfos.size(); i++){
    string s = musicinfos[i];
    int cnt =0, s_t, e_t;
    string title, content;
    while((pos= s.find(','))!=string::npos){
    if(cnt==0){
    string temp = s.substr(0, pos);
    s_t = 60*stoi(temp.substr(0,2))+stoi(temp.substr(3,2));
    }
    else if(cnt==1){
    string temp = s.substr(0,pos);
    e_t = 60*stoi(temp.substr(0,2))+stoi(temp.substr(3,2));
    }
    else title = s.substr(0,pos);
    cnt++;
    s = s.substr(pos+1);
    }
    content = s;
    while((pos = content.find('#'))!=string::npos){
    content[pos-1]=content[pos-1]+10;
    content = content.substr(0,pos)+content.substr(pos+1);
    }
    string temp ="";
    for(int i=0; i<(e_t-s_t)/content.size(); i++){
    temp+=content;
    }
    temp+=content.substr(0, (e_t-s_t) % content.size());
    if(temp.find(m)!=string::npos){
    if(max_time<e_t-s_t){
    max_time = e_t-s_t;
    answer = title;
    }
    }
    }
    if(max_time ==-1)answer ="(None)";
    return answer;
    }

     

     

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

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

    programmers.co.kr

    [프로그래머스 Level_2 / C++ / 카카오] 방금그곡

    반응형

    댓글