Published on

๐Ÿ› ๏ธ[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]-๋ฐฉ๊ธˆ ๊ทธ ๊ณก

๐Ÿ“–๋ฌธ์ œ ์„ค๋ช…


๋ผ๋””์˜ค๋ฅผ ์ž์ฃผ ๋“ฃ๋Š” ๋„ค์˜ค๋Š” ๋ผ๋””์˜ค์—์„œ ๋ฐฉ๊ธˆ ๋‚˜์™”๋˜ ์Œ์•…์ด ๋ฌด์Šจ ์Œ์•…์ธ์ง€ ๊ถ๊ธˆํ•ด์งˆ ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ๋„ค์˜ค๋Š” ๋‹ค์Œ ํฌํ„ธ์˜ '๋ฐฉ๊ธˆ๊ทธ๊ณก' ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ณค ํ•œ๋‹ค. ๋ฐฉ๊ธˆ๊ทธ๊ณก์—์„œ๋Š” TV, ๋ผ๋””์˜ค ๋“ฑ์—์„œ ๋‚˜์˜จ ์Œ์•…์— ๊ด€ํ•ด ์ œ๋ชฉ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค์ด๋‹ค.

๋„ค์˜ค๋Š” ์ž์‹ ์ด ๊ธฐ์–ตํ•œ ๋ฉœ๋กœ๋””๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฐฉ๊ธˆ๊ทธ๊ณก์„ ์ด์šฉํ•ด ์Œ์•…์„ ์ฐพ๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ผ๋””์˜ค ๋ฐฉ์†ก์—์„œ๋Š” ํ•œ ์Œ์•…์„ ๋ฐ˜๋ณตํ•ด์„œ ์žฌ์ƒํ•  ๋•Œ๋„ ์žˆ์–ด์„œ ๋„ค์˜ค๊ฐ€ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋Š” ๋ฉœ๋กœ๋””๋Š” ์Œ์•… ๋๋ถ€๋ถ„๊ณผ ์ฒ˜์Œ ๋ถ€๋ถ„์ด ์ด์–ด์„œ ์žฌ์ƒ๋œ ๋ฉœ๋กœ๋””์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ํ•œ ์Œ์•…์„ ์ค‘๊ฐ„์— ๋Š์„ ๊ฒฝ์šฐ ์›๋ณธ ์Œ์•…์—๋Š” ๋„ค์˜ค๊ฐ€ ๊ธฐ์–ตํ•œ ๋ฉœ๋กœ๋””๊ฐ€ ๋“ค์–ด์žˆ๋‹ค ํ•ด๋„ ๊ทธ ๊ณก์ด ๋„ค์˜ค๊ฐ€ ๋“ค์€ ๊ณก์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋„ค์˜ค๋Š” ๊ธฐ์–ตํ•œ ๋ฉœ๋กœ๋””๋ฅผ ์žฌ์ƒ ์‹œ๊ฐ„๊ณผ ์ œ๊ณต๋œ ์•…๋ณด๋ฅผ ์ง์ ‘ ๋ณด๋ฉด์„œ ๋น„๊ตํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ€์ •์„ ํ•  ๋•Œ ๋„ค์˜ค๊ฐ€ ์ฐพ์œผ๋ ค๋Š” ์Œ์•…์˜ ์ œ๋ชฉ์„ ๊ตฌํ•˜์—ฌ๋ผ.

๋ฐฉ๊ธˆ๊ทธ๊ณก ์„œ๋น„์Šค์—์„œ๋Š” ์Œ์•… ์ œ๋ชฉ, ์žฌ์ƒ์ด ์‹œ์ž‘๋˜๊ณ  ๋๋‚œ ์‹œ๊ฐ, ์•…๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋„ค์˜ค๊ฐ€ ๊ธฐ์–ตํ•œ ๋ฉœ๋กœ๋””์™€ ์•…๋ณด์— ์‚ฌ์šฉ๋˜๋Š” ์Œ์€ C, C#, D, D#, E, F, F#, G, G#, A, A#, B 12๊ฐœ์ด๋‹ค. ๊ฐ ์Œ์€ 1๋ถ„์— 1๊ฐœ์”ฉ ์žฌ์ƒ๋œ๋‹ค. ์Œ์•…์€ ๋ฐ˜๋“œ์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์žฌ์ƒ๋˜๋ฉฐ ์Œ์•… ๊ธธ์ด๋ณด๋‹ค ์žฌ์ƒ๋œ ์‹œ๊ฐ„์ด ๊ธธ ๋•Œ๋Š” ์Œ์•…์ด ๋Š๊น€ ์—†์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ˜๋ณตํ•ด์„œ ์žฌ์ƒ๋œ๋‹ค. ์Œ์•… ๊ธธ์ด๋ณด๋‹ค ์žฌ์ƒ๋œ ์‹œ๊ฐ„์ด ์งง์„ ๋•Œ๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์žฌ์ƒ ์‹œ๊ฐ„๋งŒํผ๋งŒ ์žฌ์ƒ๋œ๋‹ค. ์Œ์•…์ด 00:00๋ฅผ ๋„˜๊ฒจ์„œ๊นŒ์ง€ ์žฌ์ƒ๋˜๋Š” ์ผ์€ ์—†๋‹ค. ์กฐ๊ฑด์ด ์ผ์น˜ํ•˜๋Š” ์Œ์•…์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ์—๋Š” ๋ผ๋””์˜ค์—์„œ ์žฌ์ƒ๋œ ์‹œ๊ฐ„์ด ์ œ์ผ ๊ธด ์Œ์•… ์ œ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์žฌ์ƒ๋œ ์‹œ๊ฐ„๋„ ๊ฐ™์„ ๊ฒฝ์šฐ ๋จผ์ € ์ž…๋ ฅ๋œ ์Œ์•… ์ œ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์กฐ๊ฑด์ด ์ผ์น˜ํ•˜๋Š” ์Œ์•…์ด ์—†์„ ๋•Œ์—๋Š” โ€œ(None)โ€์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โœ๏ธ๋ฌธ์ œ ํ’€์ด


๊ตฌ์กฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์‹ค ๊ตฌ์กฐ์ฒด ์‚ฌ์šฉ์ด ํ’€์ด๋ฒ•์ด ๋˜๊ธฐ๋ณด๋‹ค๋Š” ๋ช‡๋ช‡ ์ฃผ์˜์‚ฌํ•ญ์„ ์—ผ๋‘์— ๋‘๊ณ  ํ‘ธ๋Š”๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ํ’€์ด์ค‘ ๋งˆ์ฃผํ•œ ๋ช‡๋ช‡ ํŠน์ด ๊ณ ๋ ค์‚ฌํ•ญ์„ ์ž‘์„ฑํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ์ฃผ์–ด์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜ musicinfos์—์„œ ๊ฐ’์„ ์ถ”์ถœํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋“ค ์ค‘ ๋ฐ˜์Œ ์˜ฌ๋ฆผ #์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
  • ๊ฐœ์ธ์ ์œผ๋กœ ๋งž๋‹ฅํŠธ๋ฆฐ ๊ณจ์น˜์•„ํŒ ๋˜ ๋ฌธ์ œ์‚ฌํ•ญ
    • ๋ฌธ์ž์—ด์„ ์ž˜๋ผ ๊ตฌ์กฐ์ฒด์— ๋Œ€์ž…ํ•˜๊ธฐ์ „์— temp์— ์ž ์‹œ ๋ณด๊ด€ํ•œ ๋’ค ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค.
    • ์ผ์น˜ํ•˜๋Š” ๋ชฉ๋ก์ด ์—†์„๊ฒฝ์šฐ ์žŠ์ง€ ์•Š๊ณ  (None)์„ ๋ฆฌํ„ด ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

โŒจ๏ธํ’€์ด ์ฝ”๋“œ


#include <string>
#include <algorithm>
#include <vector>

using namespace std;

struct song
{
    int time;
    string name;
    string code;
    bool answer = false;
};

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

    song *list = new song[musicinfos.size()];

    string nowm = "";
    for(int i = 0; i < m.size(); i++) {
        if(m[i+1] == '#'){
            nowm += tolower(m[i]);
            i++;
        }
        else {
            nowm += m[i];
        }
    }
    string temp;

    for(int i = 0; i < musicinfos.size(); i++){
        temp = "";

        int starthour = stoi(musicinfos[i].substr(0, 2));
        int endhour = stoi(musicinfos[i].substr(6, 2));
        int startmin = stoi(musicinfos[i].substr(3, 2));
        int endmin = stoi(musicinfos[i].substr(9, 2));

        list[i].time = (endhour - starthour) * 60 + (endmin - startmin);
        int current = musicinfos[i].std::string::find(",",6)+1;
        int namelast = musicinfos[i].std::string::find(",",12);
        int namelength = namelast - current;
        list[i].name = musicinfos[i].substr(musicinfos[i].std::string::find(",",6)+1, namelength);
        list[i].code = musicinfos[i].substr(musicinfos[i].std::string::find(",",12)+1);

        int cycle = list[i].time;

        for(int j = 0; j < cycle; j++){
            if(list[i].code[(j%list[i].code.size())+1] == '#'){
                temp += tolower(list[i].code[j % list[i].code.size()]);
                j++;
                cycle++;
            }
            else {
                temp += list[i].code[j % list[i].code.size()];
            }
        }
        list[i].code = temp;

        if(list[i].code.find(nowm) != string::npos) {
            list[i].answer = true;
        }

    }
    int maxtime = 0;
    int answerindex = 0;

    for (int i = 0; i < musicinfos.size(); i++){
        if(list[i].time > maxtime && list[i].answer == true) {
            maxtime = list[i].time;
            answerindex = i;
            answer = list[answerindex].name;
        }
    }

    return answer;
}

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2018 ์นด์นด์˜ค ๋ฐฉ๊ธˆ ๊ทธ ๊ณก ์ถ”์ฒœ ๋ฌธ์ œ