본문 바로가기
코딩테스트 준비/백준

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

by 스테디코디스트 2023. 7. 19.
반응형

<문제 소개>


<소스 코드>

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main()
{
	int result = 0;

	int N, M;
	cin >> N >> M;

	map<string, bool> map;

	for (int i = 0; i < N; i++)
	{
		string s;
		cin >> s;

		map.insert({ s, true });
	}

	for (int i = 0; i < M; i++)
	{
		string s;
		cin >> s;

		if (map[s]) result++;
	}

	cout << result;

	return 0;
}

<풀이과정>

1. N과 M을 입력받고, string과 bool형으로 이루어진 map을 선언

2. N만큼 반복하면 string s를 입력받고 해당 문자열과 true의 값을 가진 키와 값의 쌍을 map에 원소로 삽입

3. N만큼 원소삽입이 끝난뒤 M만큼 반복하면서 string s를 입력받고 해당 문자열을 키값으로 하여 map에 대입

4. 해당 값이 true일 경우에만 결과값 result를 증가시킴

5. 모든 검사가 끝난 뒤 result를 출력


<코멘트>

맵을 사용해서 풀었더니 쉽게 풀렸다!


<제출결과>