본문 바로가기

Algorithm_BOJ(백준)/해시

[백준 14425 c++ O] 문자열 집합

728x90
반응형

- 풀이 링크:

https://github.com/xhaktmchl/Algorithm_study/blob/main/BOJ/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0/%ED%95%B4%EC%8B%9C/%5B%EB%B0%B1%EC%A4%80%2014425%20c%2B%2B%20O%5D%20%EB%AC%B8%EC%9E%90%EC%97%B4%20%EC%A7%91%ED%95%A9.cpp

 

GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이

알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.

github.com

#include <iostream>
#include <algorithm> // fill_n, min,max, swap
#include <map> // 중복 x 
//#include <string> // getline
//#include <cstring> // memset, strok, strstr
//#include <vector>
//#include <queue> // priority_queue<자료형, 구현체, 비교 연산자>
//#include <set> // 트리, 중복 x
//#include <unordered_set>
//#include <cmath>
using namespace std;
/*
[백준 14425 c++ O] 문자열 집합
문제: 
접근: 
시간복잡도: O(n)
풀이1: 
    //1.입력
    //2.해시맵에 저장
    //3.출력: 갯수
*/
int n, m,cnt=0;
string na;
map<string, int> ma;

void input() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> na;
        ma.insert({ na,1 });
    }
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
    //1.입력
    input();
    //2.해시맵에 저장
    //3.출력: 갯수
    for (int i = 0; i < m; i++) {
        cin >> na;
        if (ma.find(na) != ma.end()) cnt += 1;
    }
    cout << cnt << '\n';
    return 0;
}
반응형