Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3-YIM2UL2ET #9

Merged
merged 4 commits into from
Feb 21, 2024
Merged

3-YIM2UL2ET #9

merged 4 commits into from
Feb 21, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Feb 18, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

BOJ 2504 - ๊ด„ํ˜ธ์˜ ๊ฐ’

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2h + ฮฑ

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์„ค๋ช…

์ด ๋ฌธ์ œ๋Š” ์šฐ์„  ๋ฌธ์ž์—ด str์„ ๋ฐ›์•„์„œ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€ ํŒ๋‹จํ•˜๊ณ , ์ด์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฉด ๋ฌธ์ž์—ด์— ๋Œ€ํ•˜์—ฌ ์—ฐ์‚ฐํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด 0์„ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
x๋ฅผ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์—ฐ์‚ฐ์— ๊ด€๋ จํ•œ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์‹ ๊ฐ’
( x ) x ๋Œ€์‘ ๊ฐ’ * 2
[ x ] x ๋Œ€์‘ ๊ฐ’ * 3
() 2
[] 3

2. ๋ฌธ์ œ ๋ถ„์„

1. ์˜ณ์€ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ

์šฐ์„  ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์˜ณ์€์ง€ ํ™•์ธ์„ ํ•ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” boolํ˜•์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ ๋งˆ์ง€๋ง‰์— ์กฐ๊ฑด๋ฌธ์œผ๋กœ 0์„ ์ถœ๋ ฅํ• ์ง€, ์—ฐ์‚ฐ ๊ฐ’์„ ์ถœ๋ ฅํ• ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์˜ณ์€์ง€๋Š” ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํƒ์˜ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์€ ์ฐพ์•„๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ๋‚˜์˜ค๋ฏ€๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์Šคํƒ์—๋Š” (๋‚˜ [ ๋งŒ์„ push ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.

KakaoTalk_20240218_185838176
1-a. ๋งŒ์•ฝ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๋น„๊ต ๋„์ค‘ ์Šคํƒ์˜ top์ด (์ผ๋•Œ )์™€ ๋Œ€์‘, ๋˜๋Š” [์ผ๋•Œ ]์™€ ๋Œ€์‘๋˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

KakaoTalk_20240218_185838176_01
1-b. ํ•˜์ง€๋งŒ (์ด ]์™€ ๋Œ€์‘๋˜๊ฑฐ๋‚˜, [์ด )์™€ ๋Œ€์‘๋˜๋ฉด ๋‹ค์Œ ๋ถˆ์™„์ „ํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

KakaoTalk_20240218_185838176_02
1-c. ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๋•Œ ) ์ด๋‚˜ ]๊ฐ€ ๋จผ์ € ๋‚˜์˜จ๋‹ค๋ฉด ๋‹ค์Œ ๋ถˆ์™„์ „ํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

KakaoTalk_20240218_185838176_03
1-d. ๋˜ํ•œ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊นŒ์ง€ ๋น„๊ต ํ›„ ์Šคํƒ์— ๋ฌธ์ž๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด ๋‹ค์Œ ๊ด„ํ˜ธ ๋ถˆ์™„์ „ํ•œ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ํ”„๋กœ์„ธ์Šค๋ฅผ bool ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜์ธ is_Cstr๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด true, ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด false๋กœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์กฐ์ •ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋ด…์‹œ๋‹ค. ๋œป์€ is collect string ์ž…๋‹ˆ๋‹ค.

is_Cstr = true;
std::string str;
std::stack <char> stk;

std::cin >> str;

for (int i = 0; i < str.size(); i++) {
    if (str[i] == '(' || str[i] == '[') stk.push(str[i]);
    else if (!stk.empty() && ((str[i] == ')' && stk.top() == '(') || (str[i] == ']' && stk.top() == '['))) stk.pop();
    else {
        is_Cstr = false;
        break;
    }
}

if (!stk.empty) is_Cstr = false;

2. ์—ฐ์‚ฐ ๋ฐฉ๋ฒ•

์ด์ œ ๋ฌธ์ž์—ด ํŒ๋‹จ์€ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ์ƒ๊ฐํ–ˆ์œผ๋‹ˆ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ผ ๋•Œ ์–ด๋–ป๊ฒŒ ์—ฐ์‚ฐ์„ ํ• ์ง€ ๋„˜์–ด๊ฐ€ ๋ด…์‹œ๋‹ค.
๋จผ์ € ์ž„์˜์˜ ๊ด„ํ˜ธ์— ๋žญํฌ๋ฅผ ๋งค๊ฒจ๋ด…์‹œ๋‹ค. ๋žญํฌ์˜ ์˜๋ฏธ๋Š” ๋ฐ”๊นฅ์ชฝ์— ๋‘˜๋Ÿฌ ์Œ“์ธ ๊ด„ํ˜ธ์˜ ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

KakaoTalk_20240218_184213021

vec์„ ๊ฐ ๋žญํฌ์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ด๋ผ๊ณ  ํ•  ๋•Œ vec์˜ ์ตœ์†Œ ํฌ๊ธฐ๋Š” ์Šคํƒ์˜ ํฌ๊ธฐ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์˜ค๋ฉด ์Šคํƒ์˜ ํฌ๊ธฐ๊ฐ€ ์กฐ์ •๋˜๋ฏ€๋กœ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

if (str[i] == '(' || str[i] == '[') {
    stk.push(str[i]);
    if (stk.size() > vec.size()) vec.push_back(0);
}

๊ทธ ๋‹ค์Œ์— ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์™”์„ ๋•Œ ์Šคํƒ์„ popํ•˜๊ธฐ ์ „ ์—ฐ์‚ฐ์„ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋จผ์ € ๊ด„ํ˜ธ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ value๊ฐ’์„ ์„ค์ •ํ•ฉ์‹œ๋‹ค. ()๋Š” 2, []๋Š” 3์œผ๋กœ value๊ฐ’์„ ์„ค์ • ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€์š”.

๊ทธ ํ›„ ๋‹ค์Œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์ด ์—†์„ ๋•Œ = vec์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์Šคํƒ์˜ ์‚ฌ์ด์ฆˆ์™€ ๊ฐ™์„ ๋•Œ
    ์ด ๋•Œ๋Š” ๊ด„ํ˜ธ ์•ˆ์ชฝ์— ๊ณฑํ•  ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ vec์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์— value๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  2. ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์ด ์žˆ์„ ๋•Œ = vec์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์Šคํƒ์˜ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ํด ๋•Œ
    ์ด ๋•Œ๋Š” vec์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ * value ์„ ๊ทธ ์ „ vec ์š”์†Œ์— ๋”ํ•ด์ฃผ๊ณ  vec๋ฅผ pop ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

2๋ฒˆ ์—ฐ์‚ฐ์—์„œ๋งŒ vec์„ pop ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— vec์˜ ์‚ฌ์ด์ฆˆ์™€ ์Šคํƒ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๋น„๊ตํ•˜์—ฌ ์—ฐ์‚ฐ์˜ ๋ฐฉ์‹์„ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

KakaoTalk_20240218_184213021_01
KakaoTalk_20240218_184213021_02

์ด๋ฅผ ์ƒ๊ฐํ•˜๋ฉฐ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

else if (!stk.empty() && ((str[i] == ')' && stk.top() == '(') || (str[i] == ']' && stk.top() == '['))){
    int value;
    if (stk.top() == '(') value = 2;
    else value = 3;

    if (stk.size() < vec.size()) {
        vec[vec.size()-2] += vec.back() * value;
        vec.pop_back();
    }
    else vec[vec.size()-1] += value;

    stk.pop();
}

๋žญํฌ๊ฐ€ 0์ด๋ฉด ๋ฐ”๊นฅ์ชฝ์— ๋” ์ด์ƒ ๊ด„ํ˜ธ๊ฐ€ ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ pop์ด ๋  ์ผ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ข…์ ์œผ๋ก  vector[0]๊ฐ€ ๊ฐ’์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

3. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>
#include <stack>
#include <vector>

int main(void)
{
    bool is_Cstr = true;
    std::string str;
    std::stack <char> stk;
    std::vector <int> vec;

    std::cin >> str;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '(' || str[i] == '[') {
            stk.push(str[i]);
            if (stk.size() > vec.size()) vec.push_back(0);
        }
        else if (!stk.empty() && ((str[i] == ')' && stk.top() == '(') || (str[i] == ']' && stk.top() == '['))){
            int value;
            if (stk.top() == '(') value = 2;
            else value = 3;

            if (stk.size() < vec.size()) {
                vec[vec.size()-2] += vec.back() * value;
                vec.pop_back();
            }
            else vec[vec.size()-1] += value;

            stk.pop();
        }
        else {
            is_Cstr = false;
            break;
        }
    }

    if (!is_Cstr || !stk.empty()) std::cout << 0;
    else std::cout << vec[0];
    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์Šคํƒ์„ ๋ฐฐ์šธ ๊ฒธ C++์— stack ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๊ธธ๋ž˜ ์‚ฌ์šฉํ•ด ๋ดค๋Š”๋ฐ vector๋กœ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์ด๋ฆ„์ด๋‚˜ ์‚ฌ์šฉ๋ฒ•์ด ๊ฐ„๋‹จํ•ด์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค. ์Šคํƒ ์ „๋ฐ˜์„ ์•Œ๊ณ  ๋‚˜๋‹ˆ ๊ด„ํ˜ธ ๋ฌธ์ œ๋“ค์€ ํ™•์‹คํžˆ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€ ์ž˜ ๋ณด์ด๋„ค์š”. ๋‹ค๋งŒ ์•„์ง์€ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ์กฐ๊ธˆ ๋” ์—ฐ์Šตํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์„ค๋ช…์„ ๋˜๊ฒŒ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์„ ์ด์šฉํ•˜์—ฌ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•ด์ฃผ์‹  ๊ฒƒ ๋•๋ถ„์— ์ดํ•ด๊ฐ€ ์‰ฝ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ์— Tree๋‚˜, ๋ฌธ์ž์—ด ๋Œ€์น˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‹ค๋ฅธ ์ˆ˜์‹์œผ๋กœ ๋งŒ๋“ค์–ด ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ, ๊ฒฐ๊ตญ์—” ํ’€์ด์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.
์„ค๋ช…ํ•ด์ฃผ์‹  ๊ฒƒ ์ค‘์—์„œ stack์„ ์‚ฌ์šฉํ•˜์—ฌ )๋‚˜ ]๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฌธ์ž์—ด์„ ๋นผ๋‚ด๋Š” ๊ฒƒ๊ณผ vector๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐ ์‹์„ ์™„์„ฑํ•ด ๋‚˜๊ฐ€์‹œ๋Š” ๊ฒƒ ๋ชจ๋‘ ํ›Œ๋ฅญํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ•œ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ ๋‹ค์Œ์— ์ด ๋ฐฉ๋ฒ•์„ ์ฐธ๊ณ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ˜

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack ์„ ์ด์šฉํ•œ ๊ด„ํ˜ธ ์ฒ˜๋ฆฌ๊ฐ€ ๋ฌธ์ œ์˜ ๋ณธ์งˆ์ด๋ผ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ด„ํ˜ธ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์˜ ๋ณธ์งˆ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์ด ์—†์„ ๋•Œ = vec์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์Šคํƒ์˜ ์‚ฌ์ด์ฆˆ์™€ ๊ฐ™์„ ๋•Œ
    ์ด ๋•Œ๋Š” ๊ด„ํ˜ธ ์•ˆ์ชฝ์— ๊ณฑํ•  ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ vec์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์— value๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  2. ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์ด ์žˆ์„ ๋•Œ = vec์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์Šคํƒ์˜ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ํด ๋•Œ
    ์ด ๋•Œ๋Š” vec์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ * value ์„ ๊ทธ ์ „ vec ์š”์†Œ์— ๋”ํ•ด์ฃผ๊ณ  vec๋ฅผ pop ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์œ„ ์กฐ๊ฑด๊ณผ ๊ฐ™์ด stk ๋ฐฐ์—ด๊ณผ vec ๋ฐฐ์—ด ์‚ฌ์ด์ฆˆ๋ฅผ ํ†ตํ•˜์—ฌ ๊ด„ํ˜ธ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ํ…Œํฌ๋‹‰์ด ๋‹๋ณด์˜€์Šต๋‹ˆ๋‹ค.

@YIM2UL2ET YIM2UL2ET merged commit a3f2ab5 into main Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants