[백준 26069번 C/C++] 붙임성 좋은 총총이
[백준 26069번 C/C++] 붙임성 좋은 총총이
https://www.acmicpc.net/problem/26069
해결전략
map
map은
key와 value를 가지는데
key값을 검색할 수 있기 때문에 다른 자료형보다 검색이 편리하다.
if (myMap.find("KeyName") != myMap.end())
으로 검색하면 된다.
map은 중복을 허용하지 않는다. 이 문제처럼 사람 이름에 중복이 없다면, key가 중복되면 데이터 삽입(=insert)이 되지않는 map을 사용하면 편하다.
코드
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
map<string, bool> myMap;
string input1, input2;
for(int i=0; i<n; i++){
cin >> input1 >> input2;
if(input1 == "ChongChong" || input2 == "ChongChong"){
myMap.insert({ input1, true });
myMap.insert({ input2, true });
}
if (myMap.find(input1) != myMap.end())
myMap.insert({ input2, true });
if (myMap.find(input2) != myMap.end())
myMap.insert({ input1, true });
}
//디버깅
//for (const auto& i : myMap)
// cout << i.first << " " << i.second << "\n";
cout << myMap.size();
return 0;
}
map 관련 참고
https://life-with-coding.tistory.com/305
'⭐ 코딩테스트 > 백준' 카테고리의 다른 글
[백준 11660번 C/C++] 구간 합 구하기 5 (0) | 2023.08.16 |
---|---|
[백준 17179번 C/C++] 케이크 자르기 (0) | 2023.08.11 |
[백준 2178번 C/C++] 미로 탐색 (0) | 2023.08.09 |
[백준 16493번 C/C++] 최대 페이지 수 (0) | 2023.08.08 |
[백준 9084번 C/C++] 동전 (0) | 2023.08.03 |
댓글
이 글 공유하기
다른 글
-
[백준 11660번 C/C++] 구간 합 구하기 5
[백준 11660번 C/C++] 구간 합 구하기 5
2023.08.16 -
[백준 17179번 C/C++] 케이크 자르기
[백준 17179번 C/C++] 케이크 자르기
2023.08.11 -
[백준 2178번 C/C++] 미로 탐색
[백준 2178번 C/C++] 미로 탐색
2023.08.09 -
[백준 16493번 C/C++] 최대 페이지 수
[백준 16493번 C/C++] 최대 페이지 수
2023.08.08