๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ด„ํ˜ธ ๋ณ€ํ™˜

ibelieveinme 2021. 8. 22. 16:58
728x90

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/60058

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ด„ํ˜ธ ๋ณ€ํ™˜

์นด์นด์˜ค์— ์‹ ์ž… ๊ฐœ๋ฐœ์ž๋กœ ์ž…์‚ฌํ•œ "์ฝ˜"์€ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋กœ๋ถ€ํ„ฐ ๊ฐœ๋ฐœ์—ญ๋Ÿ‰ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋ฌธ์ œ์ ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ผ๋Š” ์—…๋ฌด ๊ณผ์ œ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์†Œ์Šค๋ฅผ

programmers.co.kr

๋ฌธ์ œํ’€์ด

์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฐ™์ด ๋ฌธ์ œ์—์„œ ์•Œ๋ ค์ค€ ๊ด„ํ˜ธ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฐจ๋ก€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

 

<๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋งŒ๋“œ๋Š” ๋ฒ•>
1. ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
2. ๋ฌธ์ž์—ด w๋ฅผ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" u, v๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, u๋Š” "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"๋กœ ๋” ์ด์ƒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์–ด์•ผ ํ•˜๋ฉฐ, v๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
3. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" ์ด๋ผ๋ฉด ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
3-1. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ u์— ์ด์–ด ๋ถ™์ธ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
4. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"์ด ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
4-1. ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.
4-2. ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค.
4-3. ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž…๋‹ˆ๋‹ค.
4-4. u์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž…๋‹ˆ๋‹ค.
4-5. ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๋•Œ, ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€, ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ์ด ๊ฒฝ์šฐ๋Š” ๊ฐ๊ฐ isCorrectString() ์™€ returnBalanceStringIndex() ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ–ˆ๋‹ค.

 

bool isCorrectString(string balanace_string) ๋Š” ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ผ ๋•Œ๋Š” true๋ฅผ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ผ ๋•Œ๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ์„œ, ์—ด๋ฆฐ ๊ด„ํ˜ธ( (๋Š” + )์™€ ๋‹ซํžŒ ๊ด„ํ˜ธ( )๋Š” - )์˜ ์ˆ˜(check)๋ฅผ ์นด์šดํŠธํ•˜๋ฉด์„œ ํŒ๋‹จํ•œ๋‹ค. ์ˆ˜์˜ ๊ฐœ์ˆ˜๋งŒ ๊ฐ™์œผ๋ฉด ์•ˆ๋˜๊ณ (์˜ˆ๋ฅผ ๋“ค์–ด, ))((์˜ ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Œ) ๋งค ์ˆœ๊ฐ„ ์—ด๋ฆฐ๊ด„ํ˜ธ (๊ฐ€ ๋จผ์ € ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด -๊ฐ€ ํ•œ๋ฒˆ์ด๋ผ๋„ ๋‚˜์™€์„œ check ๋ณ€์ˆ˜๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋˜๋ฉด ๋ฐ”๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์งฐ๋‹ค.

 

int returnBalanceStringIndex(string original_string) ๋Š” ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด๊นŒ์ง€์˜ index๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ u, v๋กœ ๋‚˜๋ˆ„๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ๋Š” ์—ด๋ฆฐ๊ด„ํ˜ธ์™€ ๋‹ซํžŒ ๊ด„ํ˜ธ์˜ ์ˆ˜๋งŒ ๋งž์œผ๋ฉด ๋˜๋ฏ€๋กœ, ๋ฌธ์ž์—ด์˜ 0 index๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ check ๋ณ€์ˆ˜๊ฐ€ 0์ด ๋  ๋•Œ ํ˜„์žฌ index๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด u, v๋กœ ๋‚˜๋ˆ„๋Š” ๋ถ€๋ถ„์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.


balance_string1 = original_string.substr(0, returnBalanceStringIndex(original_string) + 1);
balance_string2 = original_string.substr(balance_string1.length());

original_string.substr(0, returnBalanceStringIndex(original_string) + 1) ๋Š”
0๋ถ€ํ„ฐ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ๋ฌธ์ž์—ด์ธ๋ฐ ๊นŒ์ง€๋ฅผ

 

original_string.substr(balance_string1.length()) ๋Š” ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ๋ฌธ์ž์—ด balance_string1 ๋‹ค์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

๋‚˜๋จธ์ง€๋Š” ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋ฐฉ๋ฒ•์„ ์ฐจ๋ก€๋กœ ์ ์€ ๊ฒƒ ๋ฐ–์— ์—†๊ณ ,

 

//โ˜… 4 - 4. balance_string1์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž„
balance_string1 = balance_string1.substr(1, balance_string1.length() - 2);

 

์ด ๋ถ€๋ถ„์€ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ํ•ต์‹ฌ์ด์—ˆ๋Š”๋ฐ, balance_string1.substr(1, balance_string1.length() - 2) ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ () ์ฒ˜๋Ÿผ length๊ฐ€ 2์ธ ๊ฒฝ์šฐ์—๋„ "" ๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์•„๋งˆ๋„ ์ธ๋ฑ์Šค๊ฐ€ ์ž˜๋ชป(?)๋˜๋ฉด or ๋’ค ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ ์œผ๋ฉด ""๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋“ฏ ํ•˜๋‹ค.

 

์ฝ”๋“œ

#include <string>
#include <iostream>
using namespace std;

bool isCorrectString(string balanace_string) {
    if (balanace_string == "") return true;

    int check = 0; // ( ๋Š” +1๋กœ )๋Š” -1๋กœ ํ‘œํ˜„ํ•ด์„œ ๊ด„ํ˜ธ๊ฐœ์ˆ˜ํ™•์ธ์„ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๋ฌธ์ž์—ด์ธ์ง€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.
    for (int i = 0; i < balanace_string.length(); i++) {
        if (check < 0) return false; //ํ•œ๋ฒˆ์ด๋ผ๋„ ์Œ์ˆ˜๊ฐ€ ๋˜๋ฉด )๊ด„ํ˜ธ๊ฐ€ ๋จผ์ € ์—ด๋ฆฐ ๊ฒƒ์ด๋ฏ€๋กœ false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
        check += (balanace_string[i] == '(') ? 1 : -1;
    }return check == 0 ? true : false; //๋งˆ์ง€๋ง‰์— check ๋ณ€์ˆ˜๊ฐ€ 0์ด ๋˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๋ฌธ์ž์—ด์ธ ๊ฒƒ!
}

int returnBalanceStringIndex(string original_string) {
    int check = 0; // ( ๋Š” +1๋กœ )๋Š” -1๋กœ ํ‘œํ˜„ํ•ด์„œ ๊ด„ํ˜ธ๊ฐœ์ˆ˜ํ™•์ธ์„ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๋ฌธ์ž์—ด์ธ์ง€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.
    for (int i = 0; i < original_string.length(); i++) {
        check += (original_string[i] == '(') ? 1 : -1;
        if (check == 0) return i;
    }return original_string.length();
}

string solution(string original_string) {    
    //1. ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ด๊ฑฐ๋‚˜ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
    if (original_string == "" || isCorrectString(original_string)) {
        return original_string; 
    }

    //์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ์„œ ๊ด„ํ˜ธ ๋ณ€ํ™˜์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
    //2. ๋ฌธ์ž์—ด์„ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" balance_string1, balance_string2๋กœ ๋ถ„๋ฆฌ
    string balance_string1 = "", balance_string2 = "";
    balance_string1 = original_string.substr(0, returnBalanceStringIndex(original_string) + 1);
    balance_string2 = original_string.substr(balance_string1.length());

    //3. ๋ฌธ์ž์—ด balance_string1๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" ์ด๋ผ๋ฉด ๋ฌธ์ž์—ด balance_string2์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰
    if (isCorrectString(balance_string1)) {
        //3-1. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ balance_string1์— ์ด์–ด ๋ถ™์ธ ํ›„ ๋ฐ˜ํ™˜
        balance_string1 += solution(balance_string2);
        return balance_string1;
    }

    //4. ๋ฌธ์ž์—ด balance_string1๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"์ด ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰
    else {
        string correct_string = "";

        //4 - 1. ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž„
        correct_string += "(";

        //4 - 2. ๋ฌธ์ž์—ด balance_string2์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž„
        correct_string += solution(balance_string2);

        //4 - 3. ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž„
        correct_string += ")";

        //โ˜… 4 - 4. balance_string1์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž„
        balance_string1 = balance_string1.substr(1, balance_string1.length() - 2);

        for (int i = 0; i < balance_string1.length(); i++) {
            balance_string1[i] = (balance_string1[i] == '(') ? ')' : '(';
        }
        correct_string += balance_string1;

        //4 - 5. ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜
        return correct_string;
    }
}

//test์šฉ ์ฝ”๋“œ ์ถ”๊ฐ€..
int main() {
    string original_string = "";
    cin >> original_string;
    cout << solution(original_string);
}
728x90