출처: https://3months.tistory.com/307 [Deep Play]

백준/String

[BOJ] 22232 가희와 파일 탐색기

코딩하는 랄뚜기 2021. 8. 30. 19:10

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

 

22232번: 가희와 파일 탐색기

첫 번째 줄에 jo_test 폴더에 있는 파일 개수 N과 가희가 설치한 OS에서 인식하는 파일 확장자의 개수 M이 공백으로 구분되어 주어집니다. 2번째 줄부터 N+1번째 줄까지 FILENAME.EXTENSION 형식의 문자열

www.acmicpc.net

쉬워 보이지만 시간이 많이 걸렸던 문제이다.

sort에 cmp함수에는 아무리 작은 계산이라도 넣으면 시간 초과가 난다는 것을 잊지 말자.

 

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
using namespace std;

vector<pair<pair<string,string>,int > > all;
map<string,int> ex;

bool cmp(pair<pair<string,string>,int > a,pair<pair<string,string>,int > b){
    if(a.first.first==b.first.first){
        if(a.second==b.second) return a.first.second<b.first.second;
        return a.second<b.second;
    }
    return a.first.first<b.first.first;
}

int main(){
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
    int N,M;
    cin>>N>>M;
    for(int i=0;i<N;i++){
        string s;
        cin>>s;
        int idx = s.find('.');
        all.push_back(make_pair(make_pair(s.substr(0, idx),s.substr(idx + 1)),0));
    }
    for(int i=M;i>0;i--){
        string s;
        cin>>s;
        ex[s]=-1;
    }
    for(int i=0;i<N;i++){
        if(ex[all[i].first.second]==-1){
            all[i].second=-1;
        }
    }
    sort(all.begin(),all.end(),cmp);
    
    for(int i=0;i<N;i++){
        cout<<all[i].first.first<<"."<<all[i].first.second<<'\n';
    }
    
    return 0;
}

'백준 > String' 카테고리의 다른 글

[BOJ] 2154 수 이어 쓰기 3  (0) 2021.09.07
[BOJ] 1748 수 이어 쓰기 1  (0) 2021.09.07
[BOJ] 1515 수 이어 쓰기  (0) 2021.09.06
[BOJ] 5430 AC  (0) 2021.09.06
[BOJ] 1316 그룹 단어 체커  (0) 2021.09.06