map을 써서  입력 값에 따라 원하는 동작을 바로 실행하도록 하기!

오프닝 넘기기는 아마 조건 검색을 해야할거같은데 그냥 앞뒤로 이동은 map으로 실행하기

#include <string>
#include <vector>
#include <map>
//#include <iostream>
using namespace std;

string solution(string video_len, string pos, string op_start, string op_end, vector<string> commands) {
    
    string v[4] = {video_len, pos, op_start, op_end};
    int num[4] = {0,}; //array 순서대로 len, p, ost, oed;
    
    for(int i =0; i<4; ++i){
        num[i] = stoi(v[i].substr(0, 2))*60+stoi(v[i].substr(3, 2));
    } // 계산 편하게 초 단위로 변경하기
    
    // for(auto x : num) cout << x << ", "; 변경 잘 됐는지 확인
    
    // 명령어 입력 시 수행할 행동 map으로 설정
    map <string, int> m = {{"next",10}, {"prev", -10}};
    
    int p = num[1]; // 확인 편하게 지금 위치 p 로 설정
    if(p >= num[2] && p <= num[3]) p = num[3]; // 오프닝 건뛰
    
    // commands 수행
    for(auto x : commands){
        p+=m[x]; // 동작 수행
        
        p = p<0? 0 : p>num[0]? num[0] : p; 
        // 0보다 작아지면 0, 영상 길이보다 길어지면 영상 길이, 그 외는 원래값으로 저장
        if(p >= num[2] && p <= num[3]) p = num[3];
        // 오프닝 구간 안으로 들어오면 오프닝 끝으로 자동 넘기기
    }
    
    string ans = p<600? "0":""; // ans 설정. 분단위 두자리수 / 그 외 구분
    ans += to_string(p/60);
    
    ans += (p%60) <10? ":0" : ":"; // ans 초단위 두자리수 / 그 외 구분
    ans += to_string(p%60);
    
    return ans;
}

하고 나서 보니까 굳이 맵으로 안했어도 될거같긴 하다.

+ Recent posts