```
백준 2776번 암기왕 C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 2776번 풀이
- 간단한 맵 연습
https://www.acmicpc.net/problem/2776
2776번: 암기왕
연종이는 엄청난 기억력을 가지고 있다. 그래서 하루 동안 본 정수들을 모두 기억 할 수 있다. 하지만 이를 믿을 수 없는 동규는 그의 기억력을 시험해 보기로 한다. 동규는 연종을 따라 다니며,
www.acmicpc.net
백준 2776번 암기왕은
난이도 실버 등급의 문제로서
테스트 케이스만큼 반복하며
수첩 1에 1,000,000개 미만의 정수를 입력받고
수첩 2에 1,000,000개 미만의 정수를 입력받을 때
수첩 2에 입력받은 순서대로
수첩 2에 적은 숫자가 수첩 1에 있으면 1 없으면 0을 출력하는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
여러 방법이 있고
생각나는 간단한 방법인 set을 이용하거나 배열을 이용해도 충분하게
풀 수 있지만
오늘의 맵이 당겨서
맵을 사용 했습니다.
CMyMap 클래스 선언해서
입력받고
group 1 Vec과 group 2 Vec 벡터로 나눠서 입력받은 다음에
std::unordered_map <std::int32_t, int32_t> myMap;이라는 해쉬 맵을 선언해서
group 1 벡터에 있는 값을 돌면서 myMap에 해당되는 위치를 1로 바꿔주고
group 2 벡터를 돌면서 myMap에 있으면 1 없으면 0을 출력해주었습니다
전체 코드는 아래와 같습니다.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>
#include <unordered_map>
// 특징
// 1. 많은 자료를 저장
// 2. 검색 속도 빠름
// 3. 자료 삽입, 삭제가 횟수가 적음
class CMyMap
{
private:
int32_t group1Len;
int32_t group2Len;
std::vector<int32_t> group1Vec;
std::vector<int32_t> group2Vec;
std::string solStr;
public:
CMyMap()
{
group1Len = 0;
group2Len = 0;
group1Vec.clear();
group2Vec.clear();
}
~CMyMap()
{
}
void findSolution() {
solStr = "";
// 해쉬맵 선언
std::unordered_map<std::int32_t, int32_t> myMap;
std::vector<int32_t>::iterator it = group1Vec.begin();
for (; it != group1Vec.end(); ++it) {
myMap[*it] = 1;
}
it = group2Vec.begin();
for (; it != group2Vec.end(); ++it) {
if (myMap[*it] == 1)
{
solStr+= "1\n";
}
else
{
solStr += "0\n";
}
}
}
void printSolution() {
std::cout << solStr;
}
// Setting Function
void setGroup1VecUsingStr(std::string inputStr)
{
group1Vec.clear();
std::stringstream ss(inputStr);
while (!ss.eof())
{
uint32_t elem;
ss >> elem;
group1Vec.push_back(elem);
}
}
void setGroup2VecUsingStr(std::string inputStr)
{
group2Vec.clear();
std::stringstream ss(inputStr);
while (!ss.eof())
{
uint32_t elem;
ss >> elem;
group2Vec.push_back(elem);
}
}
void setGroup2VecUsingStr(const std::vector<int32_t>& _group2Vec)
{
group2Vec.assign(_group2Vec.begin(), _group2Vec.end());
}
// Setter
void setGroup1Len(const int32_t _group1Len)
{
this->group1Len = _group1Len;
}
void setGroup2Len(const int32_t _group2Len)
{
this->group2Len = _group2Len;
}
void setGroup1Vec(const std::vector<int32_t>& _group1Vec)
{
group1Vec.assign(_group1Vec.begin(), _group1Vec.end());
}
void setGroup2Vec(const std::vector<int32_t> &_group2Vec)
{
group2Vec.assign(_group2Vec.begin(), _group2Vec.end());
}
};
int main()
{
// 입력 최적화
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
int32_t testCaseNo = std::stoi(inputStr);
for (int32_t i = 0; i < testCaseNo; i++)
{
CMyMap *cMyMap = new CMyMap();
std::getline(std::cin, inputStr);
cMyMap->setGroup1Len(std::stoi(inputStr));
std::getline(std::cin, inputStr);
cMyMap->setGroup1VecUsingStr(inputStr);
std::getline(std::cin, inputStr);
cMyMap->setGroup2Len(std::stoi(inputStr));
std::getline(std::cin, inputStr);
cMyMap->setGroup2VecUsingStr(inputStr);
cMyMap->findSolution();
cMyMap->printSolution();
delete cMyMap;
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
C++ Primer
Introduction to Algorithms
https://codemasterkimc.tistory.com/35
C++ 이론을 배울수 있는 곳 정리
개요 C++을 배우는 책, 강의, 블로그, 링크 등을 공유합니다. (링크 및 간략한 설명을 하였으나 만약 원작자가 링크를 거는것을 원치 않을 경우 연락주시기 바랍니다.) 서적 https://www.amazon.com/Prime
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'C++ > C++ 알고리즘' 카테고리의 다른 글
| 백준 2947번 나무조각 C++ 구현해보기 (1) | 2021.10.04 |
|---|---|
| 백준 17300번 패턴 C++ 구현해보기 (0) | 2021.10.02 |
| 백준 17362번 수학은 체육과목입니다2 C++ 구현해보기 (0) | 2021.09.25 |
| 백준 15624번 피보나치 수 7 C++ 구현해보기 (0) | 2021.09.25 |
| 백준 10807번 개수 세기 C++ 구현해보기 (0) | 2021.09.25 |