포스트

[ 프로그래머스 ] [3차] 방금그곡

문제


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

풀이


주어진 곡과 시간 정보 string을 분리하여 조건대로 나누었다. 재생 시간을 분으로 치환하여 비교할 문자를 찾고, find를 통해 일치 여부를 판단한다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;


string Exchange(string stringBuffer, string finding, string target)
{
    size_t pos;
    while ((pos = stringBuffer.find(finding)) != std::string::npos)
    {
        stringBuffer.replace(pos, finding.size(), target);
    }

    return stringBuffer;
}

string solution(string m, vector<string> musicinfos)
{
    string answer = "";
    vector<string> start;
    vector<string> end;
    vector<string> song;
    vector<string> lyrics;

    m = Exchange(m, "C#", "c");
    m = Exchange(m, "D#", "d");
    m = Exchange(m, "F#", "f");
    m = Exchange(m, "G#", "g");
    m = Exchange(m, "A#", "a");

    for (int i = 0; i < musicinfos.size(); i++)
    {
        string str = musicinfos[i];
        istringstream ss(str);
        string stringBuffer;

        getline(ss, stringBuffer, ',');
        start.push_back(stringBuffer);

        getline(ss, stringBuffer, ',');
        end.push_back(stringBuffer);

        getline(ss, stringBuffer, ',');
        song.push_back(stringBuffer);

        getline(ss, stringBuffer, ',');

        stringBuffer = Exchange(stringBuffer, "C#", "c");
        stringBuffer = Exchange(stringBuffer, "D#", "d");
        stringBuffer = Exchange(stringBuffer, "F#", "f");
        stringBuffer = Exchange(stringBuffer, "G#", "g");
        stringBuffer = Exchange(stringBuffer, "A#", "a");
        
        lyrics.push_back(stringBuffer);
    }



    vector<string> resize_song;

    for (int i = 0; i < musicinfos.size(); i++)
    {
        string str = start[i];
        istringstream ss(str);
        string stringBuffer;

        getline(ss, stringBuffer, ':');
        string hour = stringBuffer;
        getline(ss, stringBuffer, ',');
        string minute = stringBuffer;

        int start_hour = stoi(hour);
        int start_minute = stoi(minute);

        string str1 = end[i];
        istringstream ss1(str1);
        string stringBuffer1;

        getline(ss1, stringBuffer1, ':');
        int end_hour = stoi(stringBuffer1);
        getline(ss1, stringBuffer1, ':');
        int end_minute = stoi(stringBuffer1);

        int time = (end_hour - start_hour) * 60 + end_minute - start_minute;

        int realLyricSize = lyrics[i].size();

        if (time != realLyricSize)
        {
            string resizing;

            int cnt = time / realLyricSize;
            int rest = time % realLyricSize;

            for (int j = 0; j < cnt; j++)
            {
                resizing += lyrics[i];
            }

            if (rest != 0)
            {
                resizing += lyrics[i].substr(0, rest);
            }

            resize_song.push_back(resizing);

        }

        else
        {
            resize_song.push_back(lyrics[i]);
        }


    }

    int ans_radioSize = 0;

    for (int i = 0; i < musicinfos.size(); i++)
    {
        if (resize_song[i].find(m) != string::npos)
        {
            int a = resize_song[i].rfind(m);
            int j = 0;

            for (; j < m.size(); j++)
            {
                if (m[j] == resize_song[i][j + a])
                {
                    continue;
                }

                else
                {
                    break;
                }
            }

            if (j == m.size())
            {
                if (ans_radioSize < resize_song[i].size())
                {
                    answer = song[i];
                    ans_radioSize = resize_song[i].size();
                }
            }

        }

    }

    if (answer.size() == 0)
    {
        answer = "(None)";
    }

    return answer;
}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.