- string.find()함수와 string::npos
- find함수는 string에 특정 문자열이 있는지 확인하고 그 문자열의 시작 index를 반환한다. 문자열이 없을 경우 쓰레기 값인 string::npos를 반환한다.
- sort(strat, end, compare)
- compare함수를 통해 vector를 우리가 원하는 기준으로 정렬할 수 있다
- #include< alogrithm >이 필요하다.
네오가 기억하는 멜로디를 가지고 방금그곡 프로그램을 이용해 음악을 찾는다. 방금그곡은 다음과 같은 가정이 있다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
///vector sorting을 위한 compare함수
bool cmp(pair<string, int> a, pair<string, int> b) {
return a.second > b.second;
}
string solution(string m, vector<string> musicinfos) {
string answer = "";
//분리시킨 musicinfos를 음악별로 제목, 시간, 악보를 저장할 vector
vector<vector<string>> info(musicinfos.size());
//정답에 해당하는 제목을 저장한다.
vector<pair<string, int>> included;
// ','을 기준으로 구분 후 info에 저장
for (int i = 0; i < musicinfos.size(); i++) {
string temp = "";
for (int j = 0; j < musicinfos[i].length(); j++) {
if (musicinfos[i][j] == ',') {
info[i].push_back(temp);
temp = "";
}
else
temp += musicinfos[i][j];
}
info[i].push_back(temp);
}
/* A# C# D# F# G# 는 각각 a,c,d,f,g 로 변경 */
// m 의 변경
for (int i = 0; i < m.length(); ) {
if (m[i] == '#') {
m[i - 1] = tolower(m[i - 1]);
m.erase(m.begin() + i);
}
else
i++;
}
// info 의 변경
for (int i = 0; i < info.size(); i++) {
for (int j = 0; j < info[i][3].length(); ) {
if (info[i][3][j] == '#') {
info[i][3][j - 1] = tolower(info[i][3][j - 1]);
info[i][3].erase(info[i][3].begin() + j);
}
else
j++;
}
}
for (int i = 0; i < info.size(); i++) {
// 시간(분) 구하기
int hours = stoi(info[i][1].substr(0, 2)) - stoi(info[i][0].substr(0, 2));
int minutes = stoi(info[i][1].substr(3, 2)) - stoi(info[i][0].substr(3, 2));
int time = 60 * hours + minutes;
// 원곡의 음 중 라디오에서 실제 재생된 음
int n = info[i][3].length();
if (time > n) {
string temp = "";
for (int count = 0; count < time; count++)
temp += info[i][3][count % n];
info[i][3] = temp;
}
else if (time < n)
info[i][3] = info[i][3].substr(0, time);
// 기억하는 음을 포함하는지 여부
n = info[i][3].length();
if (m.length() > n)
continue;
if (info[i][3].find(m) != string::npos)
included.push_back(make_pair(info[i][2], time));
}
if (included.empty())
return "(None)";
sort(included.begin(), included.end(), cmp);
answer = included[0].first;
return answer;
}
출처 : [C++로 풀이] 방금그곡 ⭐⭐
지금 까지 여러가지 문제를 풀어봤지만, 스스로 부족함을 느끼고 내가 지금까지 하드코딩을 했다고 느낄 수 있었던 문제었다.