🎁문제링크
개인정보 수집 유효기간
🎁문제 풀이
접근 순서
- 날짜 계산을 편하게 하기 위해 today와 privacies의 날짜 형식을 일 단위로 계산한다.
- 약관 알파벳에 접근해서 기간을 구해야 하므로 공백으로 자르고, map을 활용한다.
🎁전체 풀이 코드
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
int year, month, day, total;
map<char, int> t_map;
vector<int> priv;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
year = stoi(today.substr(0, 4)); month = stoi(today.substr(5, 2)); day = stoi(today.substr(8, 2));
total = year * 12 * 28 + (month - 1) * 28 + day;
for(int i = 0; i < terms.size(); i++){
stringstream ss(terms[i]);
char alpha; int month;
ss >> alpha >> month;
t_map[alpha] = month;
}
for(int i = 0; i < privacies.size(); i++){
int y = stoi(privacies[i].substr(0, 4));
int m = stoi(privacies[i].substr(5, 2));
int d = stoi(privacies[i].substr(8, 2));
char a = privacies[i].back();
int tmp = y * 12 * 28 + (m - 1) * 28 + d + (t_map[a] * 28 - 1);
priv.push_back(tmp);
}
for(int i = 0; i < priv.size(); i++){
if(priv[i] < total){
answer.push_back(i + 1);
}
}
return answer;
}