[프로그래머스 C++] [3차] 방금그곡
[프로그래머스 C++] [3차] 방금그곡
https://school.programmers.co.kr/learn/courses/30/lessons/17683
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해결전략
문자열 검색
처음 시도한 코드 - 실패
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> result; string solution(string m, vector<string> mu) { int n = mu.size(); string answer = "(None)"; for(int i=0; i<n; i++) { int minutes = (stoi(mu[i].substr(6, 2)) - stoi(mu[i].substr(0, 2))) * 60 + stoi(mu[i].substr(9, 2)) - stoi(mu[i].substr(3, 2)); string name = ""; int j = 12; while(true){ if (mu[i][j] == ',') break; name += mu[i][j]; j++; } j++; int noteStart = j; string note = ""; int t = 0; int order = 0, dif = 2147000000; bool found = false; int min = minutes; while(min--) { if (j == mu[i].size()) j = noteStart; if (mu[i][j] == m[t]) { if (dif < order - t) break; if (dif == 2147000000) dif = order - t; t++; } //note += mu[i][j]; j++; order++; if (t == m.size()){ found = true; break; } } if (found == true) { result.push_back({ minutes, name }); } } if(result.size() > 0) { sort(result.begin(), result.end()); answer = result[0].second; } return answer; }
정답 코드
#include <string> #include <vector> #include <algorithm> using namespace std; // '#'이 붙은 음표를 소문자로 변환하는 함수 string convert(string m) { string ret = ""; // 반환할 문자열 초기화 for (int i = 0; i < m.size(); i++) { // m의 모든 문자를 순회 if (m[i + 1] == '#') { // 다음 문자가 '#'인 경우 ret += tolower(m[i]); // 현재 문자를 소문자로 바꿔서 ret에 추가 i++; // '#' 문자는 처리했으므로 다음 문자로 넘어감 } else { ret += m[i]; // '#'이 아니면 그대로 ret에 추가 } } return ret; // 결과 문자열 반환 } // 특정 멜로디가 어떤 곡에서 나왔는지 찾는 함수 string solution(string m, vector<string> mu) { m = convert(m); // m을 convert 함수로 전처리 string answer = "(None)"; // 반환할 문자열 초기화 int max_length = 0; // 가장 긴 재생 시간을 저장하는 변수 for (string str : mu) // mu의 모든 곡을 순회 { // 재생 시간 계산 int playTime = (stoi(str.substr(6, 2)) - stoi(str.substr(0, 2))) * 60 + stoi(str.substr(9, 2)) - stoi(str.substr(3, 2)); // 곡의 제목 추출 string title = str.substr(12, str.find(',', 12) - 12); // 곡의 악보 추출 후 convert 함수로 전처리 string notes = convert(str.substr(str.find(',', 12) + 1)); // 전체 악보를 재생 시간만큼 반복해서 문자열 생성 string note = ""; for (int i = 0; i < playTime; i++) { note += notes[i % notes.size()]; } // 만들어진 악보 문자열에서 m이 있는지 찾음 if (note.find(m) != string::npos) { if (playTime > max_length) { // m이 있고, 재생 시간이 현재까지 찾은 곡보다 긴 경우 max_length = playTime; // 재생 시간 갱신 answer = title; // 제목 갱신 } } } return answer; }
'⭐ 코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 수식 최대화 (0) | 2023.11.23 |
---|---|
[프로그래머스 C++] 괄호 변환 (1) | 2023.11.22 |
[프로그래머스 C++] 메뉴 리뉴얼 (0) | 2023.11.20 |
[프로그래머스 C++] 무인도 여행 (0) | 2023.11.17 |
[프로그래머스 C++] 124 나라의 숫자 (0) | 2023.11.16 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스 C++] 수식 최대화
[프로그래머스 C++] 수식 최대화
2023.11.23 -
[프로그래머스 C++] 괄호 변환
[프로그래머스 C++] 괄호 변환
2023.11.22 -
[프로그래머스 C++] 메뉴 리뉴얼
[프로그래머스 C++] 메뉴 리뉴얼
2023.11.20 -
[프로그래머스 C++] 무인도 여행
[프로그래머스 C++] 무인도 여행
2023.11.17
댓글을 사용할 수 없습니다.