728x90
문자열 처리 문제입니다.
N개의 문자들과 M개의 문자들에서
같은 문자를 찾아서 개수와 문자를 출력해야 합니다.
vector v1에 N개의 문자들을 전부 넣고 정렬해줍니다.
그리고 M개의 문자를 찾을 때 binary_search를 통해
vector v1안의 값과 비교를 합니다.
같을 경우 vecotr v2에 문자를 넣어줍니다.
최종적으로 vector v2를 정렬하고
v2의 사이즈를 출력한 후, 문자들을 출력해주었습니다.
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int n, m;
string s;
vector<string> v1;
vector<string> v2;
cin >> n >> m;
v1.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v1[i];
}
sort(v1.begin(), v1.end());
for (int i = 0; i < m; ++i) {
cin >> s;
if (binary_search(v1.begin(), v1.end(), s)) {
v2.push_back(s);
}
}
sort(v2.begin(), v2.end());
cout << v2.size() << endl;
for (int i = 0; i < v2.size(); ++i) {
cout << v2[i] << endl;
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 10816번 숫자 카드 2 (0) | 2019.09.09 |
---|---|
[C++] 백준 1920번 수 찾기 (0) | 2019.09.09 |
[C++] 백준 1032번 명령 프롬프트 (0) | 2019.09.09 |
[C++] 백준 1100번 하얀 칸 (0) | 2019.09.09 |
[C++] 백준 10808번 알파벳 개수 (0) | 2019.09.09 |