1764_듣보잡

bgy·2022년 1월 12일
0

백준

목록 보기
21/21

정렬되어 있는 문자도 이진탐색 가능 -> 탐색시간 줄일 수 있음
binary_search(v.begin(),v.end(),key) : 값이 존재하면 true 아니면 false 리턴 #include<algorithm>

count(v.begin(), v.end(), key)
count_if(v.begin(), v.end(), 조건)
find(v.begin(), v.end(), key)
find_if(v.begin(), v.end(), 조건)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;
int main() {
    int n,m;
    vector<string> result;
    cin >> n>>m;
    vector<string> s;
    for (int i = 0; i < n; i++) {
        string temp;
        cin >> temp;
        s.push_back(temp);
        
    }
    sort(s.begin(), s.end());
    for (int i = 0; i < m; i++) {
        string temp;
        cin >> temp;
        if (binary_search(s.begin(),s.end(),temp)) {
            
            result.push_back(temp);
        }
    }
    
    sort(result.begin(), result.end());
    cout<<result.size()<<endl;
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << endl;
        
    }
}

0개의 댓글