얼레벌레

[C++] 프로그래머스 - 개인 정보 수집 유효기간 (Lv.1) 본문

Coding/코딩테스트 준비

[C++] 프로그래머스 - 개인 정보 수집 유효기간 (Lv.1)

__Lucie__ 2023. 1. 15. 12:42

서론

에이블스쿨에서 친해진 사람들이랑 토요일마다 코테 스터디를 시작했다.

뒷심 없는 나에게 너무 필요한 모임..

말도 잘 통하고 같이 있으면 즐거운데다가 할 땐 하는 사람들이라서 스터디하는 게 너무 행복하다. 과연 한 달 뒤에도 그럴진..

 

 

문제 설명

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

 

프로그래머스

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

programmers.co.kr

오늘 날짜(today 문자열)가 주어지고, 각 약관의 유효기간 벡터(terms)가 주어진다. 

그러면 개인정보 벡터(privacies)를 받아, 오늘 삭제해야 할 개인정보를 정답 벡터에 넣어 반환한다.

 

풀이

한 달을 28일 씩만 있다고 가정해서 수월해진 문제였다.

나는 년, 월, 일을 죄다 일로 변환해서 해결했다.

ex) 2020년 12월 28일 => 2020*12*28 + 12*28 + 28 일

 

이런 문제를 만날 때마다 까다로운 게 문자열 처리였는데,

다른 팀원들은 python을 사용해 문자열 처리가 수월해 보였다. (파이썬의 큰 장점 중 하나 인듯!)

c++은 stringstream이 프로그래머스에서 문자열 처리하기 좋은 STL이라 요즘 문자열 처리에서 stringstream을 애용하고 있다.

 

 

코드

#include <string>
#include <vector>
#include <iostream>

using namespace std;


vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    // today의 길이
    
    int today_year = stoi(today.substr(0, 4));
    int today_month = stoi(today.substr(5, 2));
    int today_day = stoi(today.substr(8));
    
    int term[30] = {0, };
    
    // 각 약관 별 유효기간 저장
    for(int i=0; i<terms.size(); i++){
        
        char term_name = terms[i][0];
        int due_date = stoi(terms[i].substr(2));
        
        term[term_name-'A'] = due_date*28;
    }
    
    long today_long = today_year*12*28 + today_month*28 + today_day;
    
    for(int i=0; i<privacies.size(); i++) {
        
        int tmp_year = stoi(privacies[i].substr(0, 4));
        int tmp_month = stoi(privacies[i].substr(5, 2));
        int tmp_day = stoi(privacies[i].substr(8));
        
        char tmp_term_name = privacies[i][11];
        
        long tmp_long = tmp_year*12*28 + tmp_month*28 + tmp_day;
        
        long diff = today_long - tmp_long;
        
        if(diff>=term[tmp_term_name-'A']) answer.push_back(i+1);
        else continue;
        
        
    }
    
    
     return answer;
}

 

제출 결과