728x90
A수열과 B수열이 있을 때
A수열에 B수열의 값이 몇개 있는지 찾는 문제입니다.
A수열을 vector에 전부 넣고 sort해줍니다.
그리고 upper_bound - lower_bound로
B수열의 수를 A수열에서 찾으면 개수가 나옵니다.
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int num;
int n, m;
int count;
scanf("%d", &n);
vector<int> v(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &v[i]);
}
sort(v.begin(), v.end());
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%d", &num);
count = upper_bound(v.begin(), v.end(), num) - lower_bound(v.begin(), v.end(), num);
cout << count << " ";
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1927번 최소 힙 (0) | 2019.09.09 |
---|---|
[C++] 백준 11279번 최대 힙 (0) | 2019.09.09 |
[C++] 백준 1920번 수 찾기 (0) | 2019.09.09 |
[C++] 백준 1764번 듣보잡 (0) | 2019.09.09 |
[C++] 백준 1032번 명령 프롬프트 (0) | 2019.09.09 |