https://school.programmers.co.kr/learn/courses/30/lessons/150370
일단 stringstream으로 파싱 할 수 있도록 today의 .을 공백으로 replace한다. 그 후 today를 파싱하여 연 월 일을 구하고 연월일을 따로 따로 비교하면 3번을 비교해야하니 일 수에 맞춰서 연 월값을 구해서 total에 합쳐둔다.
그 다음 terms를 파싱하여 map<string, int> 컨테이너에 넣어준다.
마지막으로 privacies를 for문을 돌려가며 파싱한다. .을 공백으로 대체, stringstream으로 연 월 일 약관종류를 파싱하고 나서 total에 합친다.
합친 날짜가 오늘날짜보다 작을경우 ( 유효기간을 지났을 경우 ) 해당 인덱스를 answer에 넣어준다.
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
replace(today.begin(), today.end(), '.', ' ');
int t_y, t_m, t_d;
stringstream t_ymd;
t_ymd.str(today);
t_ymd >> t_y >> t_m >> t_d;
int t_total = t_y * 12 * 28 + t_m * 28 + t_d;
map<string,int> term;
for(auto& t : terms)
{
stringstream ss;
string s;
int num;
ss.str(t);
ss >> s >> num;
term[s] = num * 28;
}
for(int i = 0; i < privacies.size(); i++)
{
replace(privacies[i].begin(), privacies[i].end(), '.', ' ');
stringstream ss;
string s;
int p_y, p_m, p_d;
ss.str(privacies[i]);
ss >> p_y >> p_m >> p_d >> s;
int p_total = p_y * 12 * 28 + p_m * 28 + p_d - 1 + term[s];
if(p_total < t_total)
answer.push_back(i + 1);
}
return answer;
}
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
int year = stoi(today.substr(0, 4));
int month = stoi(today.substr(5, 2));
int days = stoi(today.substr(8));
int todayD = (year)*12 * 28 + (month - 1) * 28 + days;
vector<int> ar(privacies.size());
map<char, int> mp;
for (int i = 0; i < terms.size(); i++) {
stringstream ss(terms[i]);
char c;
int month;
ss >> c >> month;
mp[c] = 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 c = privacies[i].back();
ar[i] = (y)*12 * 28 + (m - 1) * 28 + d + mp[c] * 28 - 1;
}
for (int i = 0; i < ar.size(); i++) {
if (ar[i] < todayD) {
answer.push_back(i + 1);
}
}
return answer;
}
today를 stringstream이 아닌 substr한 값을 stoi로 파싱함.
terms는 map에다가 파싱하여 넣음
privacies는 today처럼 파싱하였고 값 비교는 total로 비교함.
바로바로 비교하지 않고 파싱을 전부 한 뒤 비교해서 answer 완성