반응형
728x170
https://school.programmers.co.kr/learn/courses/30/lessons/17683
문자열 처리를 위한 문제다.
C++로는 어렵지만 stringstream으로 헤쳐나가보자 중요하다.
c++의 문자열 처리는 stringstream에게 맡기라고
그리고 #도 한글자로 쳐야하는 처리를 해주는게 까다롭다.
#맞는 풀이
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
struct mus{
int playtime;
int idx;
string title;
};
bool comp(mus a, mus b){
if(a.playtime == b.playtime){
return a.idx < b.idx;
}
return a.playtime > b.playtime;
}
string solution(string m, vector<string> musicinfos) {
string answer = "";
vector<mus> vec;
int seq = 0;
for(auto music : musicinfos){
int start;
int end;
string title;
string melody;
stringstream ss(music);
int hh,mm;
string temp;
//start
getline(ss, temp, ':');
hh = stoi(temp);
getline(ss, temp, ',');
mm = stoi(temp);
start = hh*60 + mm;
//end
getline(ss, temp, ':');
hh = stoi(temp);
getline(ss, temp, ',');
mm = stoi(temp);
end = hh*60 + mm;
//title
getline(ss,title,',');
//melody
getline(ss,melody,',');
//cout << start <<" "<< end << " "<< title << " " << melody << '\n';
//시간동안 재생된 멜로디 전체
string total = "";
int totalTime = end - start;
//C와 C#은 하나로 쳐야함
int idx = 0; //노래는 처음부터시작
//시간당 1개씩 넣어줘야함
for(int i = 0; i<totalTime; i++){
idx %= melody.size();
//#은 연주한 것이 아니라 앞의 것과 붙어있는 것
if(melody[idx] == '#'){
idx++;
i--;
continue;
}
total += melody[idx];
if(melody[idx+1] == '#'){
total += '#';
}
idx++;
}
//cout << total << '\n';
bool isCheck = false;
int length = m.length(); //#이 1개로 포함되어있음 #의 개수는 빼야함
int cnt=0;
for(char c : m){
if(c == '#')cnt++;
}
length -= cnt;
//재생시간이 짧은 경우, total 길이가 m보다 짧을 수 있음 => 비교불가(None)
if(total.size() >= length){
for(int i = 0; i<total.size()-length+1; i++){
//문자 그대로 비교하면 안됨(#이 있기 때문)
string xx = "";
int idx = i;
for(int j = 0; j<length; j++){
xx += total[idx];
if(total[idx+1] == '#'){
idx+=2;
xx+='#';
}else idx++;
}
cout << xx <<'\n';
if(xx == m){
isCheck = true;
break;
}
}
}
//조건에 맞는 음악일 경우
if(isCheck){
mus tmp;
tmp.playtime = totalTime;
tmp.idx = seq;
tmp.title = title;
vec.push_back(tmp);
}
seq++;
}
sort(vec.begin(), vec.end(), comp);
if(vec.empty()){
answer = "(None)";
}else{
answer = vec.front().title;
}
return answer;
}
728x90
반응형
그리드형
'문제풀이(Problem Solving)' 카테고리의 다른 글
백준, BOJ, 5582번, 공통 부분 문자열 C++ [CPP] ★★★ (1) | 2022.09.25 |
---|---|
프로그래머스, 셔틀버스, C++ [CPP] ★★★★ (1) | 2022.09.20 |
프로그래머스, 파일명 정렬, C++ [CPP] ★★★★★ (1) | 2022.09.15 |
프로그래머스, 프렌즈4블록, C++ [CPP] ★★★★ (0) | 2022.09.14 |
프로그래머스, 압축, C++ [CPP] ★★★★ (0) | 2022.09.14 |