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

semi·2022년 7월 12일
0

coding test

목록 보기
20/57

https://www.acmicpc.net/problem/14425

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main(void)
{
	int N, M;
	int cnt = 0;
	string str;
	vector<string> s, v;
	cin >> N >> M;
	for (int i = 0; i < N; i++)
	{
		cin >> str;
		s.push_back(str);
	}
	sort(s.begin(), s.end());
	for (int i = 0; i < M; i++)
	{
		cin >> str;
		v.push_back(str);
	}
	int left_idx, right_idx, mid_idx;
	for (int i = 0; i < M; i++)
	{
		left_idx = 0;
		right_idx = N - 1;
		string target = v[i];
		while (left_idx <= right_idx)
		{
			mid_idx = (left_idx + right_idx) / 2;
			if (s[mid_idx] == target)
			{
				cnt++;
				break;
			}
			else if (s[mid_idx] < target)
			{
				left_idx = mid_idx + 1;
			}
			else // s[mid_idx] > target
			{
				right_idx = mid_idx - 1;
			}
		}
	}
	cout << cnt;
	
	return 0;
}

0개의 댓글