๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Summer/Winter Coding ์Šคํ‚ฌํŠธ๋ฆฌ level2

by Jouureee 2021. 5. 8.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/49993

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์Šคํ‚ฌํŠธ๋ฆฌ

 

programmers.co.kr

 

๋ถ„์„ :

๋ถ„๋ช… ๋‚˜๋ณด๋‹ค ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค! 

ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” ๋‹คํ–‰ํžˆ(?) ํšจ์œจ์„ฑ ์ฒดํฌํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์—ฌ์„œ ๋งž์ถœ ์ˆ˜ ์žˆ์—ˆ๋‹ค. (soma ์ฒซ ๊ฒŒ์ž„ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ผ(๊ทผ๋ฐ ๊ทธ ์นœ๊ตฌ๋Š” ์œ„์ƒ ์ •๋ ฌ์ด์—ˆ๋‹ค) ๊ผญ ํ’€๊ณ  ์‹ถ์—ˆ๋‹ค)

 

์ด ๋ฌธ์ œ๋Š” ์ „์— ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ณต๋ถ€ํ–ˆ์„๋•Œ ์–ธ๋œป ๋ดค์—ˆ๋˜ ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๊ณผ ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ skill์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์Šคํ‚ฌ ํŠธ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์—์„œ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค.

๋งŒ์•ฝ์— ์ œ์‹œ๋œ skill ์ˆœ์„œ๊ฐ€ CBD ์ด๊ณ  ์ˆœ์„œ์— ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ์‹ถ์€ ๋Œ€์ƒ๋“ค {"CBADF", " AECB", "OPQ", "CED"}๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ 

๋‹ต์€ {"CBADF", " AECB"} ๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰ ์ˆœ์„œ์—๋งŒ ๋งž์ถ”๋ฉด ๋˜๋Š” ๊ฒƒ์ด์ง€ skill์ด ๊ฐ€์ง„ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ skillTree๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ ์ ‘๊ทผํ•œ ๋ฐฉ์‹์€ 

1. skillTree์˜ ๊ฐ ๋ฌธ์ž์—ด์— ์ ‘๊ทผํ•ด skill์— ์žˆ๋Š” ๋ฌธ์ž์—ด๋งŒ re_skillTree ๋ฐฐ์—ด์— ๋‹ด๊ธฐ

2. re_skillTree์— ๋Œ€ํ•˜์—ฌ size๊ฐ€ 0์ผ ๊ฒฝ์šฐ ex> "OPQ" skill ์กฐ๊ฑด์— ์œ„๋ฐฐ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ +1

3. re_skillTree์— ๋Œ€ํ•˜์—ฌ size๊ฐ€ 0์ด ์•„๋‹ ๊ฒฝ์šฐ skill๊ณผ ์ˆœ์„œ๊ฐ€ ๋งž๋Š”์ง€ ๊ฒ€์‚ฌ

 

3๋ฒˆ์—์„œ re_skillTree๋Š” skill์— ์žˆ๋Š” ๋ฌธ์ž์—ด๋งŒ ๋‹ด๊ฒผ์œผ๋ฏ€๋กœ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ์œ„๋ฐฐ๋˜๋Š” ์กฐ๊ฑด์ด๋‹ค

๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์ด ์›€์ง์ธ๋‹ค๋ฉด ๋‚˜์ค‘์— 

 

k์™€ n์˜ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋ฏ€๋กœ +1 ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค!

 

c++ ์ฝ”๋“œ :

//
//  [SW] 2018_skillTree.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/30.
//

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>


using namespace std;

int main(){
    int answer = 0;
    string skill = "CBD";
    vector<string> skillTree = {"BACDE", "CBADF", "AECB", "BDA","OPQ", "CED"};
    vector<char> re_skillTree;
    
    for(int i =0; i < skillTree.size(); i++){
        
        for(int j = 0; j < skillTree[i].size(); j++){
            if(skill.find(skillTree[i][j]) != string::npos){
                re_skillTree.push_back(skillTree[i][j]);
            }
        }
        
        //์ฃผ์–ด์ง„ ์Šคํ‚ฌ ์กฐ๊ฑด์ด ํ•˜๋‚˜๋„ ์—†์–ด๋„ ์ •๋‹ต
        if(re_skillTree.size() == 0){
            answer++;
        }else{
            //์žˆ์œผ๋ฉด ์ˆœ์„œ ๋น„๊ต
            int k = 0; int n = 0;
            for(int i = 0; i < re_skillTree.size(); i++){
                if(re_skillTree[n] == skill[k++]){
                    n++;
                }else{
                    break;
                }
            }
            if(k != 0 && n != 0 && k == n){
                answer++;
            }
        }
        
        re_skillTree.clear();

    }

    cout << answer << '\n';
    return 0;
}

 

 

๊ฒฐ๊ณผ :

๋Œ“๊ธ€