1.

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
vector<int> s;
vector<int> e;
vector<string> name;
vector<string> content;
 
// 재생된 시간도 같을 경우 먼저 입력된 음악 제목을 반환
bool cmp(pair<stringint> a, pair<stringint> b)
{
    if(a.second < b.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
string solution(string m, vector<string> musicinfos) 
{
    string answer = "";
    
    // C# -> c, D# -> d, F# -> f, G# -> g, A#-> a 변환
    for(int i = 0; i < m.size(); i++)
    {
        int idx = 0;
        
        while(m.find("#", idx) != string::npos)
        {
            idx = m.find("#", idx);
            char lower_alphabet = m[idx-1]-'A'+'a';
            string alphabet;
            alphabet += lower_alphabet;
            m.replace(idx-12, alphabet);
        }
    }
    
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int idx = 0;
        
        while(musicinfos[i].find("#", idx) != string::npos)
        {
            idx = musicinfos[i].find("#", idx);
            char lower_alphabet = musicinfos[i][idx-1]-'A'+'a';
            string alphabet;
            alphabet += lower_alphabet;
            musicinfos[i].replace(idx-12, alphabet);
        }
    }
    
    // 시작시각, 끝난시각, 제목, 악보별 정리
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int temp_start = 0;  // 시작시각을 분으로 저장
        int temp_arrive = 0// 도착시각을 분으로 저장
        
        string hour = musicinfos[i].substr(0,2); // 시
        string minute = musicinfos[i].substr(3,2); // 분
        
        temp_start = stoi(hour)*60 + stoi(minute);
        
        hour = musicinfos[i].substr(6,2);
        minute = musicinfos[i].substr(9,2);
        
        temp_arrive = stoi(hour)*60 + stoi(minute);
        
        s.push_back(temp_start);
        e.push_back(temp_arrive);
        
        int j;
        string temp_name;
        for(j = 12; musicinfos[i][j] != ','; j++)
        {
            temp_name += musicinfos[i][j];
        }
        name.push_back(temp_name);
        
        j++;
        string temp_content;
        for(int k = j; k < musicinfos[i].size(); k++)
        {
            temp_content += musicinfos[i][k];
        }
        content.push_back(temp_content);
    }
    
    int max_play = 0// 제일 긴 재생 시간 저장
    vector<pair<stringint>> reserve; // 음악 제목, 인덱스 저장
    
    // 시작입력의 갯수 = 음악의 갯수만큼 반복
    for(int i = 0; i < s.size(); i++)
    {
        int diff = e[i]-s[i];
        string total_content;
        
        for(int j = 0; j < diff; j++)
        {
            total_content += content[i][j % content[i].size()];
        }
        
        if(total_content.find(m) != string::npos)
        {
            // 조건이 일치하는 음악이 여러 개일 때에는 라디오에서 재생된 시간이 제일 긴 음악 제목을 반환
            if(diff > max_play)
            {
                reserve.clear();
                
                max_play = diff;
                reserve.push_back({name[i], i});
            }
            else if(diff == max_play)
            {
                reserve.push_back({name[i], i});
            }
        }
    }
    
    if(reserve.size() == 0)
    {
        answer = "(None)";
    }
    else if(reserve.size() == 1)
    {
        answer = reserve[0].first;
    }
    else
    {
        sort(reserve.begin(), reserve.end(), cmp);   
        answer = reserve[0].first;
    }
    
    return answer;
}
cs

 

2.

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
vector<int> s;
vector<int> e;
vector<string> name;
vector<string> content[110];
 
// 재생된 시간도 같을 경우 먼저 입력된 음악 제목을 반환
bool cmp(pair<stringint> a, pair<stringint> b)
{
    if(a.second < b.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
string solution(string m, vector<string> musicinfos) 
{
    string answer = "";
    
    // 시작시각, 끝난시각, 제목, 악보별 정리
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int temp_start = 0;  // 시작시각을 분으로 저장
        int temp_arrive = 0// 도착시각을 분으로 저장
        
        string hour = musicinfos[i].substr(0,2); // 시
        string minute = musicinfos[i].substr(3,2); // 분
        
        temp_start = stoi(hour)*60 + stoi(minute);
        
        hour = musicinfos[i].substr(6,2);
        minute = musicinfos[i].substr(9,2);
        
        temp_arrive = stoi(hour)*60 + stoi(minute);
        
        s.push_back(temp_start);
        e.push_back(temp_arrive);
        
        int j;
        string temp_name;
        for(j = 12; musicinfos[i][j] != ','; j++)
        {
            temp_name += musicinfos[i][j];
        }
        name.push_back(temp_name);
        
        j++;
        for(int k = j; k < musicinfos[i].size(); k++)
        {
            string temp_content;
            
            if(k+1 < musicinfos[i].size() && musicinfos[i][k+1== '#')
            {
                temp_content += musicinfos[i][k];
                temp_content += musicinfos[i][k+1];
                temp_content += '.'// # 구분을 위해서 . 삽입
                
                k = k+1;
                content[i].push_back(temp_content);
            }
            else
            {
                temp_content += musicinfos[i][k];
                temp_content += '.'// # 구분을 위해서 . 삽입
                
                content[i].push_back(temp_content);
            }
        }
    }
    
    // 악보의 음마다 . 삽입했기때문에 찾는 m의 사이사이마다 . 삽입한 M 생성
    string M;
    for(int i = 0; i < m.size(); i++)
    {
        if(i+1 < m.size() && m[i+1== '#')
        {
            M += m[i];
            M += m[i+1];
            M += '.';
            
            i = i+1;
        }
        else
        {
            M += m[i];
            M += '.';   
        }
    }
    
    int max_play = 0// 제일 긴 재생 시간 저장
    vector<pair<stringint>> reserve; // 음악 제목, 인덱스 저장
    
    // 시작입력의 갯수 = 음악의 갯수만큼 반복
    for(int i = 0; i < s.size(); i++)
    {
        int diff = e[i]-s[i];
        string total_content;
        
        for(int j = 0; j < diff; j++)
        {
            total_content += content[i][j % content[i].size()];
        }
        
        if(total_content.find(M) != string::npos)
        {
            // 조건이 일치하는 음악이 여러 개일 때에는 라디오에서 재생된 시간이 제일 긴 음악 제목을 반환
            if(diff > max_play)
            {
                reserve.clear();
                
                max_play = diff;
                reserve.push_back({name[i], i});
            }
            else if(diff == max_play)
            {
                reserve.push_back({name[i], i});
            }
        }
    }
    
    if(reserve.size() == 0)
    {
        answer = "(None)";
    }
    else if(reserve.size() == 1)
    {
        answer = reserve[0].first;
    }
    else
    {
        sort(reserve.begin(), reserve.end(), cmp);   
        answer = reserve[0].first;
    }
    
    return answer;
}
cs

 

+ Recent posts