프로그래머스 150370 개인정보 수집 유효기간

1 개요[ | ]

프로그래머스 150370 개인정보 수집 유효기간

2 C++[ | ]

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

bool isExpired(string today, string startDate, int months) {
    int ey = stoi(startDate.substr(0,4));
    int em = stoi(startDate.substr(5,2)) + months;
    int ed = stoi(startDate.substr(8,2))-1;
    if(ed < 1) {
        ed += 28;
        em--;
    }
    if(em < 1) {
        em += 12;
        ey--;
    } else if(em>12) {
        ey += (em-1)/12;
        em = ((em-1)%12)+1;
    }
    int ty = stoi(today.substr(0,4));
    int tm = stoi(today.substr(5,2));
    int td = stoi(today.substr(8,2));
    if(ty > ey) return true;
    if(ty == ey) {
        if(tm > em) return true;
        if(tm == em) {
            if(td > ed) return true;        
        }
    }
    return false;
}


vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    map<char,int> m;
    for(auto& t: terms) {
        char termType = t[0];
        int months = stoi(t.substr(2));
        m[termType] = months;
    }
    for(int i=0; i<privacies.size(); i++) {
        string p = privacies[i];
        string startDate = p.substr(0,10);
        char termType = p[11];
        if(isExpired(today, startDate, m[termType])) {
            answer.push_back(i+1);
        }
    }
    sort(answer.begin(), answer.end());
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}