반응형

문제출처

https://www.acmicpc.net/problem/1764


문제풀이

/**
* vector를 2개 사용해서 단순 비교한 뒤 같다면 answer에 추가해주는 방식으로 진행했더니
* 시간초과가 났다. 2개를 사용하지 않고 하나에 입력을 받아 compare해준 뒤 같다면
* answer에 추가해주는 방식으로 풀이. 사실 문제이름이 재미있어서 선택했는데
* 너무 쉬운문제였다는 것이 함정.
*/


소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n,m;
    cin>>n>>m;
    vector<string> dbj(n+m), answer;
 
    for(int i=0; i<n+m; i++)
        cin>>dbj[i];
    sort(dbj.begin(), dbj.end());
 
    for(int i=1; i<n+m; i++)
        if(dbj[i].compare(dbj[i-1]) == 0)
            answer.push_back(dbj[i]);
 
    cout<<answer.size()<<"\n";
    for(string name : answer) cout<<name<<"\n";
 
    return 0;
}
cs


반응형

'Algorithm' 카테고리의 다른 글

[BOJ 9996] 한국이 그리울 땐 서버에 접속하지  (0) 2019.02.22
[BOJ 9935] 문자열 폭발  (0) 2019.02.21
[BOJ 1213] 펠린드롬 만들기  (0) 2019.02.19
[BOJ 1431] 시리얼 번호  (0) 2019.02.18
[BOJ 1937] 욕심쟁이 판다  (0) 2019.02.15

+ Recent posts