본문 바로가기

Algorithm/Baekjoon BOJ

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

반응형

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

 

24060번: 알고리즘 수업 - 병합 정렬 1

첫째 줄에 배열 A의 크기 N(5 ≤ N ≤ 500,000), 저장 횟수 K(1 ≤ K ≤ 108)가 주어진다. 다음 줄에 서로 다른 배열 A의 원소 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 109)

www.acmicpc.net

 

 

 

 


- 문제

 

  S 문자열이 여러개 주어지면

이후에 주어지는 문자열 중에서 S에 속한 애가 몇개인지 찾는 문제다.

 

 


- 해설

 

map을 이용해서 <string, int>를 통해 string 을 키값으로 저장하면 쉽게 셀 수 있다.

 

 


- 풀이

#include <iostream>
#include <map>
#include <string>

using namespace std;

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

    map<string, int> ma;
    string temp;
    int n, m, answer = 0;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        ma[temp]++;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> temp;
        if (ma[temp] == 1)
            answer++;
    }
    cout << answer;
    return 0;
}

- 새롭게 알게 된 점

 

X

반응형