백준/해시

[백준] C++ 14425 문자열 집합

2zreal 2024. 9. 25. 12:38

이 문제는 hashSet을 사용하여 푸는 간단한 문제이다.

 

문제해결방법

1. 먼저 집합 S를 입력받는다.

2. M개의 문자열을 입력받고 집합 S에 포함되는지 확인한다. 만약 있다면 count++를 해준다.

3. count를 출력한다.

#include <iostream>
#include <unordered_set>
using namespace std;

int main(void)
{
	unordered_set<string> hashSet;
	int N = 0;
	int M = 0;
	cin >> N >> M;

	for (int i = 0; i < N; i++)//집합 S를 입력받음. 
	{
		string temp = "";
		cin >> temp;
		hashSet.insert(temp);
	}

	int count = 0;
	for (int i = 0; i < M; i++)//집합 S에 포함되는가?? 
	{
		string temp = "";
		cin >> temp;
		if (hashSet.find(temp) != hashSet.end())
		{
			count++;
		}
	}

	cout << count;
	return 0;
}