๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (๋ฌธ์ž์—ด ์••์ถ•, 2020 KAKAO BLIND RECRUITMENT) C++

by UKHYUN22 2021. 12. 29.
728x90

 

 

 

๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์žฅ๋‚œ์„ ์น˜๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. substr์„ ์ž์œ ์ž์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€, erase๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์˜ index ๋ฌธ์ œ์ ์„ ์ž˜ ํŒŒ์•…ํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๋ฌธ์ œ ๊ฐ™์•˜๋‹ค. ์‚ฌ์‹ค ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์€ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜์™€ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ์ด๋ฏ€๋กœ ์ด๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ด์„œ ๋”ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด๋„ ๋œ๋‹ค. ๋ณต์žกํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์š”๊ตฌํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์กฐ๊ฑด์„ ์ž˜ ์„ธ์›Œ์„œ ์ •๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋…ผ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int solution(string s) {
    int answer = 0;
    int total = 0;
    int count = 1;
    int min = 99999;
    string restore = s;
    string temp = "";
    
    // ๋ฌธ์ž์—ด์˜ ์ ˆ๋ฐ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ธธ์ด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.
    for(int i = 1 ; i <= s.size()/2 ; i++) {
        s = restore;
        
        // ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ Bubble sort ์ฒ˜๋Ÿผ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ for ๋ฌธ
        for(int j = 0; j < s.size() - 1 ; j = j + i) {
            
            // ์‹œ์ž‘์ ๊ณผ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ break;
            if(j+i >= s.size()) break;
            
            // j index๋ถ€ํ„ฐ i ๊ธธ์ด ๋งŒํผ temp์— ํ• ๋‹น
            temp = s.substr(j, i);
            
            // ๋งŒ์•ฝ temp์™€ ๊ทธ ๋‹ค์Œ ๋ฐ˜๋ณต ๋ฌธ์ž์—ด์ด ๊ฐ™์€ ๊ฒฝ์šฐ
            if(temp == s.substr(j+i, i)) {
                
                // ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด ์ค‘ ์•ž ๋ถ€๋ถ„์„ ์ง€์šฐ๊ณ 
                s.erase(j,i);
                
                // erase ํ•œ ๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์˜ index๋ฅผ ๋•ก๊ฒจ์ค€๋‹ค.
                j = j - i;
                
                // ๋ช‡ ๋ฒˆ ๋ฐ˜๋ณต๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
                count++;
                
                // ๋งŒ์•ฝ 3๋ฒˆ์งธ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์—์„œ ๋ฐ˜๋ณต๋จ์ด ๋Š๊ธด๋‹ค๋ฉด ํ˜„์žฌ ํƒ์ง€ํ•œ ๋ฐ˜๋ณต๋จ์ด
                // ๋งˆ์ง€๋ง‰์ด๋ฏ€๋กœ countํ•œ ๊ฒƒ์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜์‹œ์ผœ ๊ทธ ๊ธธ์ด๊ฐ€ ์–ผ๋งˆ์ธ์ง€
                // total์— ๋ˆ„์ ์‹œํ‚จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ count๋Š” 1๋กœ ์ดˆ๊ธฐํ™”
                if(j+2*i <= s.size() && s.substr(j+i,i) != s.substr(j+2*i, i)) {
                    string one = to_string(count);
                    total += one.size();
                    count = 1;
                }
            }
        }
        // total ๋ณ€์ˆ˜์— ์ž๋ฅธ ๋ฌธ์ž์—ด์˜ size์™€ total์— ๋ˆ„์ ๋œ count ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋”ํ•œ๋‹ค
        total = s.size() + total;
        
        // ๋งŒ์•ฝ ๊ทธ๊ฒƒ์ด ์ตœ์†Œ๊ฐ’์ด๋ฉด min์— ์ตœ์‹ ํ™”๋ฅผ ์‹œ์ผœ์ค€๋‹ค.
        if(min > total) {
            min = total;
        }
        
        // ์ด ์ž‘์—…์„ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ s.size()/2๋ณด๋‹ค ์ž‘์„๋•Œ๊นŒ์ง€ ์‹œํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ค€๋น„
        count = 1;
        total = 0;
    }
    
    // ๋งŒ์•ฝ min ์ด ์ตœ์‹ ํ™” ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด(๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋Š” ๊ฒฝ์šฐ)
    if(min == 99999) answer = s.size();
    else answer = min;
    
    return answer;
}