일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 리액트 네이티브 맥
- react native
- javascript
- react native mac
- 리액트 네이티브 설치 오류
- react native typescript navigate
- react native accessible
- react native typescript navigation
- 스탠실 버퍼 튜토리얼
- c++ 정보은닉
- react native typescript
- cyworld
- 스탠실 버퍼 사용
- react-native
- CSS
- 스탠실 버퍼 시작
- 벡터와 리스트의 차이
- node
- react native 타입스크립트
- unity stencil buffer
- C++
- c++ using
- Expo
- stencil buffer
- html
- react native ios 기기 연결
- GitHub
- node.js
- 싸이월드
- react
- Today
- Total
혀니의 이거저거 뿌시기
[C++]프로그래머스 개인정보 수집 유효기간 본문
코딩테스트 연습 - 개인정보 수집 유효기간 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
map<string, int> termTable;
map<int, tuple<string, string>> privTable;
void init(vector<string> terms, vector<string> privacies)
{
for(auto term: terms)
{
string alp = term.substr(0, term.find(' '));//A,B,C
string month = term.substr(term.find(' ') + 1);//유효기간
termTable.insert({alp, stoi(month)});
}
int i = 1;
for(const auto priv:privacies)
{
string date = priv.substr(0, priv.find(' '));
string alp = priv.substr(priv.find(' ') + 1);
privTable.insert({i, make_tuple(date, alp)});
i++;
}
}
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
init(terms, privacies);
int tyear = stoi(today.substr(0,4));
int tmonth = stoi(today.substr(5,2));
int tday = stoi(today.substr(8));
for(auto priv:privTable)
{
int year = stoi(get<0>(priv.second).substr(0,4));
int month = stoi(get<0>(priv.second).substr(5, 2));
int day = stoi(get<0>(priv.second).substr(8));
month += termTable[get<1>(priv.second)];
day--;
if(day == 0)
{
day = 28;
month--;
}
if(month > 12)
{
year++;
month -= 12;
}
if((tyear - year) * 28 * 12 + (tmonth - month) * 28 + (tday - day) > 0)
answer.push_back(priv.first);
}
return answer;
}
구현 아이디어는 명확한데.. 그것을 저장하거나 정렬하는 구현에서 애를 먹어서 복잡하게 풀었던 문제..
더 쉬운 방법도 있을 테지만...
나는 map의 한 키값에 두가지 이상의 값을 저장하는 방법을 알고 싶기도 해서 조금 복잡하게 풀었다.
주목할 점은 tuple이다.
unordered_map이건 map이건 저장하는 순서대로 정렬되는 것이 아니라 그 순서가 차례대로 정렬되지 않는다.
그래서
map<int, tuple<string, string>> privTable;
이런 형태로 순서와 date,alp값을 저장해야만 했다.
그 두개의 값을 묶을 때는 make_tuple을 사용해야 하며
튜플로 묶인것의 요소를 참조하고 싶을 때는 get<0>(튜플이름) 과 같이 사용해야 한다.
-----------------------------------------------------------
<알게된 점>
1.때로는 map보다 vector로 저장하는게 나을 떄가 있다.(순서가 정해져있어야 하는 경우)
이럴 때는 vector를 만들되 make_pair를 이용해서 값 저장을 하자.
2.map의 키값을 출력하고 싶을 때는 .first와 같이 하면 된다.
3.공백문자 기준으로 자를떄는 substr를 이용하자
'알고리즘 공부(C++)' 카테고리의 다른 글
[C++] 백준 1012 유기농 배추 (DFS) (0) | 2023.08.21 |
---|---|
[C++] 백준 2606 <바이러스> (0) | 2023.08.20 |
[C++]프로그래머스 [3차]n진수 게임 (0) | 2023.08.12 |
[C++]프로그래머스 징검다리 건너기(이진탐색) (0) | 2023.08.03 |
[C++]백준 1072 게임 (이진탐색) (0) | 2023.08.02 |