[c/c++] ๋ฐฑ์ค€ 14425 (Silver 3)

์€๋™ยท2023๋…„ 1์›” 25์ผ
0

Baekjoon

๋ชฉ๋ก ๋ณด๊ธฐ
12/49

๐Ÿ”จ ๋ฌธ์ œ

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

<์š”์•ฝ>

์ด N๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


๐Ÿ”จ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

  1. ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด N๊ณผ M์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 10000์ด๋‹ค. ์ด๋Ÿด ๋•Œ์—๋Š” ๋น„๊ต ๋Œ€์ƒ์ด ๋งŽ์•„์„œ binary search๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    ( O(log N) )
  • binary_search ํ•จ์ˆ˜ ( โ—๏ธ ๊ผญ ์‚ฌ์šฉ ์ „ sortํ•ด์ฃผ๋Š” ๊ฑฐ ์žŠ์ง€ ๋ง๊ธฐ )

algorithm ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ include ํ•ด์ค˜์•ผ ํ•จ
์ง์ ‘ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ ๋„ binary search ํƒ์ƒ‰ ๊ฐ€๋Šฅ
binary_search(์‹œ์ž‘์ , ๋์ , ๋น„๊ต๊ฐ’)์œผ๋กœ ์„ ์–ธํ•˜๊ณ  ๋ฐ˜ํ™˜ํ˜•์€ boolํ˜•


๐Ÿ”จ ์ฝ”๋“œ

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

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, m, cnt=0;
	cin >> n >> m;
	vector <string> word;
	string str;
	for (int i = 0; i < n; i++) {
		cin >> str;
		word.push_back(str);
	}
	sort(word.begin(), word.end());

	for (int i = 0; i < m; i++) {
		cin >> str;
		if (binary_search(word.begin(), word.end(), str)) cnt++;		
	}
	cout << cnt;

	return 0;
}

์ฝ”๋“œ ์ œ์ถœํ•˜๊ณ  ๋‚จ๋“ค์€ ์–ด๋–ป๊ฒŒ ํ–ˆ๋‚˜ ๋ณด๋‹ค๊ฐ€ unordered_map์ด๋ผ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ๊ณง ์ •๋ฆฌํ•ด์„œ ์˜ฌ๋ ค๋ด์•ผ์ง€

profile
์ž์ž ์„ ์ˆ˜์ž…์žฅ~

0๊ฐœ์˜ ๋Œ“๊ธ€