반응형
문자열을 입력받고 같은 문자열의 개수를 찾는 프로그램
n : 듣도 못한 사람의 수
m : 보도 못한 사람의 수
입력값
2 3
hduck
Amgay
dora2
ada
hduck
출력값
1
hduck
- 문자열을 sort()를 이용해서 정렬을 한다.
- 정렬후 순차적으로 하나씩 비교하여 같은 문자열의 개수를 카운트한다.
function(std::vector<T> vec;) | meaning |
vec.begin() | iterator pointing to the first element.(첫번째 원소의 위치) |
vec.end() | iterator pointing to next to last element.(마지막 원소 다음의 위치를 말한다) |
vec.push_back(value) | 벡터의 가장 마지막 위치에 value를 추가 |
vec.pop_back() | 벡터 가장 마지막 위치의 value 삭제 |
vec[i] | i번째 원소를 반환 |
vec.at(i) | i번째 원소를 반환 |
vec.front() | 첫번째 원소 반환 |
vec.back() | 마지막 원소 반환 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string name;
vector<string> names, duplicated;
int n, m;
cin >> n >> m;
for (int i = 0; i < n + m; i++) {
cin >> name;
names.push_back(name);
}
sort(names.begin(), names.end());
int cnt = 0;
for (int i = 0; i < n + m; i++) {
if (!names[i].compare(names[i + 1])) {
duplicated.push_back(names[i]);
i++;
cnt++;
}
}
cout << cnt << endl;
for (auto a : duplicated) {
cout << a << endl;
}
return 0;
}
|
cs |
반응형
'알고리즘 > 코드 연습' 카테고리의 다른 글
[백준 2445번] 별 찍기 - 8 (0) | 2020.11.27 |
---|---|
[백준 - 1316] 그룹 단어 체커 (0) | 2020.11.27 |
[백준 - 1924] 2007년 (0) | 2020.07.29 |
[백준 - 2798] 블랙잭 (0) | 2020.07.24 |
[LeetCode] Roman to Integer (0) | 2020.07.21 |