๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๋ฐฑ์ค€/C++] 14425๋ฒˆ : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

by nitronium102 2021. 9. 12.

๋ฌธ์ œ

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

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

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. 

๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค.

๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ์ง‘ํ•ฉ S์— ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์— ์ด ๋ช‡ ๊ฐœ๊ฐ€ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

ํ’€์ด

Set์„ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ

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

int main(){
    int n, m, cnt = 0;
    string input;
    set<string> s;

    cin >> n >> m;
    // 01. ์ง‘ํ•ฉ s์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด ์ž…๋ ฅ
    while (n--){
        cin >> input;
        s.insert(input);
    }

    // 02. ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด ์ž…๋ ฅ
    while(m--){
        cin >> input;
        // ์ง‘ํ•ฉ s์— ํฌํ•จ๋œ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ
        if (s.count(input))
            cnt++;
    }
    cout << cnt;
}

๋Œ“๊ธ€