#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(pair<string,int> a, pair<string,int> b){
if(a.second == b.second) return false;
return a.second > b.second;
}
string solution(string m, vector<string> musicinfos) {
string answer = "";
vector<string> mv;
for(int j = 0;j<m.length();j++) {
if(m[j] == '#')
mv[mv.size()-1] += "#";
else{
string t = "";
t += m[j];
mv.push_back(t);
}
}
if(musicinfos.empty()) return "(None)";
vector<pair<string, int>> v;
int i=0;
while(i < musicinfos.size())
{
int H = stoi(musicinfos[i].substr(6,2)) - stoi(musicinfos[i].substr(0,2));
int M = stoi(musicinfos[i].substr(9,2)) - stoi(musicinfos[i].substr(3,2));
int total = H*60 + M;
vector<string> origin;
string name = "";
int z =12;
for(;z<musicinfos[i].length();z++){
if(musicinfos[i][z] == ',') break;
name += musicinfos[i][z];
}
for(z += 1;z<musicinfos[i].length();z++) {
if(musicinfos[i][z] == '#')
origin[origin.size()-1] += "#";
else{
string t = "";
t += musicinfos[i][z];
origin.push_back(t);
}
}
vector<string> str;
for(int j=0;j<total;j++)
str.push_back(origin[j%origin.size()]);
for(int j=0;j<str.size();j++)
{
if(str[j] != mv[0]) continue;
int flag = 0;
for(int k=0;k<mv.size();k++)
{
if(str[j+k] != mv[k]) {
flag = 1;
break;
}
}
if(flag == 0)
{
v.push_back({name, total});
break;
}
}
i++;
}
if(v.empty()) answer = "(None)";
else {
sort(v.begin(), v.end(), compare);
answer = v.front().first;
}
return answer;
}
- key point!
1) #
문자에 대한 처리를 위해 vector<string>
에 #
까지 포함해서 저장
--> C#
을 c
처럼 다른 문자로 대체하여 비교하는 것도 방법임!
2) sort에서 사용하는 compare()
정의 할 때 return false
이면 교환하지 X