[프로그래머스 C++] 개인정보 수집 유효기간

 

https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

 

해결전략

 

문자열


 

 

정답코드

 

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

map<string, int> myMap;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    
    int tYear = stoi(today.substr(0,4));
    int tMonth = stoi(today.substr(5,2));
    int tDay = stoi(today.substr(8,2));
    //cout << tyear << ", " << tmonth << ", " << tday << "\n";
    
    for(int i = 0; i < terms.size(); i++){
        string alpha = terms[i].substr(0,1);
        int beta = stoi(terms[i].substr(2));
        //cout << alpha << ", " << beta << "\n";
        myMap[alpha] = beta;
    }
    
    for(int i = 0; i < privacies.size(); i++)
    {        
        int year = stoi(privacies[i].substr(0,4));
        int month = stoi(privacies[i].substr(5,2));
        int day = stoi(privacies[i].substr(8,2));
        string term = privacies[i].substr(11,1);
        //cout << year << ", " << month << ", " << day << ", " << term << "\n";
        
        int newYear = year;
        int newMonth = month;
        int newDay = day;
        
        if(myMap.find(term) != myMap.end())
        {
            if(myMap[term] == 0) continue;
            
            newMonth += myMap[term];
            
            if(newDay == 1){
                newDay = 28;
                newMonth--;
            }
            else{
                newDay--;
            }
            
            if (newMonth > 12) // 12 초과인 경우 -1 한 후 연산해야 한다.
            {
                newYear += (newMonth - 1) / 12;
                newMonth = (newMonth - 1) % 12 + 1;
            }
        }
        
        if(newYear < tYear) answer.emplace_back(i+1);
        else if(newYear == tYear && newMonth < tMonth) answer.emplace_back(i+1);
        else if(newYear == tYear && newMonth == tMonth && newDay < tDay) answer.emplace_back(i+1);
    }
    
    return answer;
}