Doby's Lab

백준 14425번: 문자열 집합 (C++) 본문

PS/BOJ

백준 14425번: 문자열 집합 (C++)

도비(Doby) 2022. 5. 2. 22:40

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

 

14425번: 문자열 집합

첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다.  다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어

www.acmicpc.net


Solved By: Map

 

map에서는 find() 메서드를 썼을 때, 해당 key값이 없다면 m.end() iterator를 반환하는 것을 알고 있다면 쉽게 구현할 수 있습니다.

 

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

map<string, int> m;

int n, m2;

int main(){
    cin >> n >> m2;
    
    for(int i = 0; i < n; i++){
        string s; cin >> s;
        m[s]++;
    }
    
    int res = 0;
    for(int i = 0; i < m2; i++){
        string s; cin >> s;
        if(m.find(s) != m.end()) res++;
    }
    
    cout << res;
    return 0;
}
728x90