최대 1 분 소요

Question

14425Q


Solution

  • Set에 n개의 문자열들을 입력 받는다.
  • m이 있는지 set::find를 사용한다.

Cord

#include <iostream>
#include <set>

using namespace std;

set<string> s;

int main()
{
    int n, m;
    int count = 0;
    string input;

    // 입력 수
    cin >> n >> m;

    // 집합에 포함되는 문자열 입력
    for (int idx = 0; idx < n; idx++)
    {
        cin >> input;
        s.insert(input);
    }

    // 검사해야하는 문자열
    for (int idx = 0; idx < m; idx++)
    {
        cin >> input;
        if (s.find(input) != s.end())
        {
            count++;
        }
    }
    cout << count;

    return 0;
}

Result

14425


What`s new

균형 이진트리로 구성되어 있는 STL 함수이다.
set<string> s

요소 추가 하기
s.inset(key);

요소 찾기
반환값은 반복자(lterator) 이다.
s.end() 는 s의 마지막 원소의 다음 원소를 가리킨다.

if (s.find(key) != s.end())
{
    // 실행
}