백준 1764 듣보잡 C++
2022. 5. 19. 12:12ㆍ알고리즘
728x90
N과 M 이 주어지는데 이때, N + M만큼 받을 때 한 배열에 동시에 다 넣고 그 배열을 n+m-1만큼 돌면서 앞의 값이랑 뒤의 값이 같으면 다른 배열에 넣고 따로 나중에 출력해주면 쉽게 풀 수 있다.
map 알고리즘으로도 푸는 방법이 있는데 나중에 풀면 따로 올리겠습니다.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector <string> str;
for (int i = 0; i < n + m; i++) {
string st;
cin >> st;
str.push_back(st);
}
sort(str.begin(), str.end());
vector <string> s;
for (int i = 0; i < n + m - 1; i++) {
if (str[i] == str[i + 1]) {
s.push_back(str[i]);
}
}
cout << s.size() << '\n';
for (int i = 0; i < s.size(); i++) {
cout << s[i] << '\n';
}
return 0;
}
728x90
'알고리즘' 카테고리의 다른 글
[C++][STL] map 사용법 정리 (0) | 2022.05.30 |
---|---|
소수구하기 에라토스테네스의 체 (0) | 2022.05.19 |
최대 공약수, 최소 공배수 구하는 문제 (0) | 2022.05.19 |
팰린드롬(회전문 문제) (0) | 2022.03.23 |
1.시간 복잡도 빅오 표기법 O(N) (0) | 2021.11.05 |