본문 바로가기
Coding Test/Programmers

[Programmers] (2018 KAKAO BLIND RECRUITMENT) Lv 2. [3차] 방금그곡

by song.ift 2023. 8. 7.

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <numeric>

using namespace std;

enum MusicInfo
{
    S,
    E,
    TITLE,
    MELODY,
};

void revertM(string& m)
{
    // # 먼저 체크하기 위해 역으로 체크
    for (auto rit = m.rbegin(); rit != m.rend(); rit++) {
        if (*rit == '#')
        {
            m.erase(m.rend() - rit - 1, 1); // # 삭제
            *rit = tolower(*(++rit)); // 대문자를 소문자로. G -> g
        }
    }
}

string solution(string m, vector<string> musicinfos) {
    string answer = "(None)";
    size_t longTime = 0;

    revertM(m);

    ::for_each(musicinfos.begin(), musicinfos.end(), [&](const auto& music) {
        istringstream ss(music);
        string subs; // temp 역할하는 substr 변수
        vector<string> v;

        while (getline(ss, subs, ','))
            v.push_back(subs); // ',' 을 기준으로 인덱스가 증가하며 string 저장

        const auto s = stoi(v[S].substr(0, 2)) * 60 + stoi(v[S].substr(3)); // start 시간
        const auto e = stoi(v[E].substr(0, 2)) * 60 + stoi(v[E].substr(3)); // end 시간
        const size_t time = e - s;

        revertM(v[MELODY]);

        string melody;
        for (size_t i = 0; i < time / v[MELODY].length(); ++i) melody += v[MELODY];
        for (size_t i = 0; i < time % v[MELODY].length(); ++i) melody += v[MELODY][i];

        if (melody.find(m) != string::npos && longTime < time)
            longTime = time, answer = v[TITLE];
    });

    return answer;
}

GitHub :https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/b3313898bc0e5824f01e3aef929d104051eb2905 

 

[level 2] Title: [3차] 방금그곡, Time: 0.48 ms, Memory: 4.14 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@

developeSHG committed Aug 6, 2023

github.com

 

댓글