๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ(Level 2)

by dkswnkk 2022. 4. 13.

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ

๋ ˆ์Šคํ† ๋ž‘์„ ์šด์˜ํ•˜๋˜ ์Šค์นดํ”ผ๋Š” ์ฝ”๋กœ๋‚˜19๋กœ ์ธํ•œ ๋ถˆ๊ฒฝ๊ธฐ๋ฅผ ๊ทน๋ณตํ•˜๊ณ ์ž ๋ฉ”๋‰ด๋ฅผ ์ƒˆ๋กœ ๊ตฌ์„ฑํ•˜๋ ค๊ณ  ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” ๋‹จํ’ˆ์œผ๋กœ๋งŒ ์ œ๊ณตํ•˜๋˜ ๋ฉ”๋‰ด๋ฅผ ์กฐํ•ฉํ•ด์„œ ์ฝ”์Šค์š”๋ฆฌ ํ˜•ํƒœ๋กœ ์žฌ๊ตฌ์„ฑํ•ด์„œ

programmers.co.kr

 

์ฝ”๋“œ

#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>


using namespace std;

map<string,int>cnt;
map<int,int>len_max;

vector<string>combi_arr;

void combination(int index, string combi, string origin, int max_len){
    for(int i=index; i<max_len; i++){
        string inp = combi+origin[i];
        combi_arr.push_back(inp);
        combination(i+1, inp, origin, max_len);
    }
}

vector<string> solution(vector<string> orders, vector<int> course) {
    vector<string> answer;
    
    for(string order:orders){
        set<string>ncr;
        
        for(int i=0; i<order.length(); i++) combination(i,  "", order, order.length());
        for(string &combi:combi_arr) sort(combi.begin(),combi.end());
        for(string combi:combi_arr) ncr.insert(combi);     
        for(auto it=ncr.begin(); it!=ncr.end(); it++) cnt[*it]++;
        
        combi_arr.clear();
    }
    
    for(auto it = cnt.begin(); it!=cnt.end(); it++){
        int len = (it->first).length();
        if(len_max[len]<it->second) len_max[len] = it->second;
    }
    for(auto it = cnt.begin(); it!=cnt.end(); it++){
        for(int cos:course){
            if(it->second>=2&&(it->first).length()==cos&&it->second==len_max[(it->first).length()]){
                answer.push_back(it->first);
            }
        }
    }
    sort(answer.begin(),answer.end());
    return answer;
}

 

ํ’€์ด(45๋ถ„ ์ด์ƒ)

์ฒ˜์Œ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์—ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋•Œ๋ฌธ์— 50์ ์ด ๋– ์„œ ๊ต‰์žฅํžˆ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>


using namespace std;

map<string,int>cnt;
map<int,int>len_max;

vector<string> solution(vector<string> orders, vector<int> course) {
    vector<string> answer;
    for(string &order:orders) sort(order.begin(),order.end());  //๋ฏธ๋ฆฌ ์ •๋ ฌ
    
    for(string order:orders){
        map<string,int>visited;
        set<string>ncr;
        do{
            string ord = order;
            while(!ord.empty()){
                ncr.insert(ord);
                ord.pop_back();
            }
        }while(next_permutation(order.begin(),order.end()));
        
        for(auto it=ncr.begin(); it!=ncr.end(); it++){
             string inp = *it;
             sort(inp.begin(),inp.end());
            if(!visited[inp]){
                cnt[inp]++;
                visited[inp]=1;
            }
        }
    }
    
    for(auto it = cnt.begin(); it!=cnt.end(); it++){
        int len = (it->first).length();
        if(len_max[len]<it->second) len_max[len] = it->second;
    }
    for(auto it = cnt.begin(); it!=cnt.end(); it++){
        for(int cos:course){
            if(it->second>=2&&(it->first).length()==cos&&it->second==len_max[(it->first).length()]){
                answer.push_back(it->first);
            }
        }
    }
    sort(answer.begin(),answer.end());
    return answer;
}

์†๋‹˜๋งˆ๋‹ค ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ข€ ํŽธํ•˜๊ฒŒ ํ•ด ๋ณด์ž๊ณ  next_permutation์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ์‹œ๊ฐ„ ์ดˆ๊ณผ์˜ ์›์ธ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋œจ์ž๋งˆ์ž ๋ฐ”๋กœ ๋ฐฑํŠธ๋ž™ํ‚น์œผ๋กœ ๊ตฌํ˜„์„ ํ•˜๋ฉด ์ข‹์•˜์„ ํ…๋ฐ ๋˜ ์“ธ๋ฐ์—†๋Š” ๊ณ ์ง‘ ๋•Œ๋ฌธ์— next_permutation์„ ์‚ฌ์šฉํ•ด์„œ ์–ต์ง€๋กœ ์–ด๋–ป๊ฒŒ๋“  ํ•ด๋ณด๋ ค๊ณ  ์ด๊ฒƒ์ €๊ฒƒ ๊ฑด๋“œ๋ฆฐ ๊ฒŒ ์‹œ๊ฐ„๋‚ญ๋น„๊ฐ€ ๋งค์šฐ ์ปธ์Šต๋‹ˆ๋‹ค..์—ํœด,.. ๊ณ ์ง‘์„ ์ข€ ์ค„์—ฌ์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€