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

11-YIM2UL2ET #36

Merged
merged 4 commits into from
Mar 29, 2024
Merged

11-YIM2UL2ET #36

merged 4 commits into from
Mar 29, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Mar 18, 2024

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

BOJ 1918 - ํ›„์œ„ํ‘œ๊ธฐ์‹

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

์•ฝ 40m

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

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

์ค‘์œ„ ํ‘œ๊ธฐ์‹์„ ์ž…๋ ฅ ๋ฐ›์•„ ํ›„์œ„ ํ‘œ๊ธฐ์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
ํ”ผ์—ฐ์‚ฐ์ž๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์—ฐ์‚ฐ์ž๋Š” *, /, +, -, (, ) ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ๋ฌธ์ œ ํ•ด๊ฒฐ

์ค‘์œ„ ํ‘œ๊ธฐ์‹์„ ํ›„์œ„ ํ‘œ๊ธฐ์‹์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ์Šคํƒ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ๋Œ€์ค‘์ ์ž…๋‹ˆ๋‹ค.
๋ฌธ์ œ ํ•ด๊ฒฐ์— ์ด์šฉํ•  stack ํด๋ž˜์Šค์™€, ๋‹ต์„ ์ถœ๋ ฅํ•  string ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์—ฌ ๋ด…์‹œ๋‹ค.
์Šคํƒ์€ stk, ๋ฌธ์ž์—ด์€ res๋ฅผ ๊ฐ์ฒด๋กœ ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค.

0. ์ž…๋ ฅ ๋ฐ ๋ผˆ๋Œ€ ์žก๊ธฐ

๋จผ์ € ์ž…๋ ฅ์„ ๋ฐ›์•„์•ผ๊ฒ ์ง€์š”. charํ˜• ๋ณ€์ˆ˜์ธ ch์— ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ ๋ฐ›์•„์„œ ํ•ด๊ฒฐํ•ฉ์‹œ๋‹ค.

char ch = getchar();
while(ch != '\n') {
    // solve
}

ch๊ฐ€ ์ž…๋ ฅ ๋ฐ›์„ ๋ฌธ์ž๋Š” ํ”ผ์—ฐ์‚ฐ์ž์™€ ์—ฐ์‚ฐ์ž, ๊ด„ํ˜ธ๋กœ ์ด ์„ธ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ค‘์œ„ ํ‘œ๊ธฐ๋ฒ•์—์„œ ํ›„์œ„ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ ์‹œ ํ”ผ์—ฐ์‚ฐ์ž๋Š” ๋ฐ”๋กœ๋ฐ”๋กœ ์ถœ๋ ฅ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. (res์— push_back)
๋ฌธ์ œ๋Š” ์—ฐ์‚ฐ์ž์™€ ๊ด„ํ˜ธ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ผ€์ด์Šค๋ฅผ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž๋Š” *,/,+,-๋กœ ์ด 4๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹น์—ฐํ•˜๊ฒŒ๋„ ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ฃ . 1์ˆœ์œ„๊ฐ€ *, / ์—ฐ์‚ฐ์ž์ด๊ณ , 2์ˆœ์œ„๊ฐ€ +,- ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.
์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ  stk์˜ top๊ณผ, ch์„ ์„œ๋กœ ๋น„๊ตํ•˜์˜€์„ ๋•Œ ์ทจํ•  ํ–‰๋™์„ ํ‘œ๋กœ ์ •๋ฆฌํ•ด๋ด…์‹œ๋‹ค.

์—ฐ์‚ฐ์ž ์ˆœ์œ„ ๋น„๊ต ์ทจํ•  ํ–‰๋™
top < ch ch๋ฅผ push
top == ch top ์ถœ๋ ฅ ํ›„ ch๋ฅผ push
top > ch stk๊ฐ€ ๋น„๊ฑฐ๋‚˜, ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ์ถœ๋ ฅ ํ›„ ch๋ฅผ push

์ด๋ฅผ *, /์™€ +, -๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ˆ˜๋„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ด…์‹œ๋‹ค.

if (ch == '*' or ch == '/') {
    if (stack.top == '*' or stack.top == '\') {
        print (stack.top)
        stack.pop
    }
    print ch
}
else if (ch == '+' or ch == '-')
    while (stk.empty or stk.top != '(') {
        print (stack.top)
        stack.pop
    }
    print ch
}

2. ๊ด„ํ˜ธ

๊ด„ํ˜ธ๋Š” ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ (์™€, ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ )๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ด„ํ˜ธ ์•ˆ์— ์žˆ๋Š” ์—ฐ์‚ฐ์ž๋Š” ๊ด„ํ˜ธ ์•ˆ์—์„œ ์—ฐ์‚ฐ์„ ๋๋‚ธ ํ›„ ๋ฐ”๊นฅ์œผ๋กœ ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ–‰๋™์„ ์ทจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ด„ํ˜ธ ์ทจํ•  ํ–‰๋™
( stk์— push
) (์ด ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ์—ฐ์‚ฐ์ž ์ถœ๋ ฅ ํ›„ pop

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

#include <iostream>
#include <stack>

int main(void)
{
    char ch;
    std::stack <char> stk;
    std::string res;

    ch = getchar();
    while (ch != '\n') {
        if (ch == '+' || ch == '-') {
            while(!stk.empty() && stk.top() != '(') {
                res.push_back(stk.top());
                stk.pop();
            }
            stk.push(ch);
        }
        else if (ch == '*' || ch == '/') {
            if (!stk.empty() && (stk.top() == '*' || stk.top() == '/')) {
                res.push_back(stk.top());
                stk.pop();
            }
            stk.push(ch);
        }
        else if (ch == ')') {
            while (!stk.empty() && stk.top() != '(') {
                res.push_back(stk.top());
                stk.pop();
            }
            if (!stk.empty()) stk.pop();
        }
        else if (ch == '(') stk.push(ch);
        else res.push_back(ch);
        ch = getchar();
    }
    while (!stk.empty()) {
        res.push_back(stk.top());
        stk.pop();
    }
    std::cout << res;
    return 0;
}

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

์Šคํƒ์„ ํ™œ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๊ณจ๋“œ2 ๋ฌธ์ œ์ง€๋งŒ ์Šคํƒ์„ ๊ณต๋ถ€ ํ–ˆ์„ ๋•Œ ๋ฐฐ์› ๋˜ ๋ฌธ์ œ๋ผ์„œ ๊ฝค๋‚˜ ์‰ฌ์šด ๋ฌธ์ œ์˜€๋„ค์š”.
์—ฌ๋‹ด์ด์ง€๋งŒ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๊ณต๋ถ€์ค‘์ธ๋ฐ ๊ตฌํ˜„์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹ค์Œ ๋ฌธ์ œ๋Š” ์ด์ง„ ํŠธ๋ฆฌ ๊ด€๋ จ ๋ฌธ์ œ๋กœ ๋Œ์•„์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

์—„์ฒญ ์ต์ˆ™ํ•œ ๋ฌธ์  ๋ฐ ์˜›๋‚ ์— ๋ดค๋‹ค ๋ณด๋‹ˆ ๊ฐ€๋ฌผ๊ฐ€๋ฌผํ•ด์„œ ๋‹ต๋‹ตํ•œ ๋งˆ์Œ์— ๋‹ต์•ˆ์„ ๋ชป ํ’€๊ณ  ๋‹ต์„ ๋ด๋ฒ„๋ ธ๋„ค์š”. ํ’€์–ด๋ด์•ผํ•˜๋Š”๋ฐ

์˜ฌ๋ ค์ฃผ์‹  ๊ธ€์„ ๋ณด๊ณ  ์ด๋Ÿฐ ๋ฌธ์ œ์˜€์ง€ ํ•˜๊ณ  ์‰ฝ๊ฒŒ ๊ธฐ์–ต์ด ๋‚ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ๊ธ€์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ +,-,*,/๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. java ์ฝ”๋“œ์ธ๋ฐ C++์ด๋ž‘ ๋น„์Šทํ•œ ๋ฌธ๋ฒ•์ด ๋งŽ์•„์„œ ์ดํ•ดํ•˜์‹œ๋Š”๋ฐ ํฐ ์–ด๋ ค์›€์€ ์—†์„๊ฑฐ ๊ฐ™์•„์š”. ์ œ ์ƒ๊ฐ์—๋Š” ์ด๊ฒŒ ๋” ์ง๊ด€์ ์ด๊ธฐ๋Š” ํ•œ ์ฝ”๋“œ ๊ฐ™์•„์„œ ๋“ค๊ณ ์™€๋ดค์Šต๋‹ˆ๋‹ค๋งŒ..
C++์—์„œ๋„ ์ด๋Ÿฐ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ๊ตณ์ด? ์‹ถ์–ด์„œ ์•ˆํ•˜๋Š”๊ฑด์ง€ ์ž๋ฃŒ๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค๋”๋ผ๊ณ ์š”. ํ˜น์‹œ ์•„์‹œ๋Š” ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”!

์šฐ์„ ์ˆœ์œ„ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ œํ’€๊ธฐ

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.

stack์„ ์•ˆ๋‹ค๋ฉด ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ฐ™์€ ๋ฌธ์ œ์ธ์ง€ ๊ธฐ์–ต์ด ๋‚˜์ง€๋Š” ์•Š์ง€๋งŒ ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋˜ ๊ฑฐ ๊ฐ™์€๋ฐ, ์ด ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ๋‹ค์Œ์— ํ•œ๋ฒˆ ๋” ์ฐพ์•„๋ณด๊ณ  ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋˜ ๋ง‰์ƒ ์ฒ˜์Œ๋ณด๋ฉด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€ ์• ๋จน์„๊ฑฐ ๊ฐ™์€ ๋ฌธ์ œ์ธ๊ฑฐ ๊ฐ™๊ธด ํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž

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

@YIM2UL2ET
Copy link
Collaborator Author

์—„์ฒญ ์ต์ˆ™ํ•œ ๋ฌธ์  ๋ฐ ์˜›๋‚ ์— ๋ดค๋‹ค ๋ณด๋‹ˆ ๊ฐ€๋ฌผ๊ฐ€๋ฌผํ•ด์„œ ๋‹ต๋‹ตํ•œ ๋งˆ์Œ์— ๋‹ต์•ˆ์„ ๋ชป ํ’€๊ณ  ๋‹ต์„ ๋ด๋ฒ„๋ ธ๋„ค์š”. ํ’€์–ด๋ด์•ผํ•˜๋Š”๋ฐ

์˜ฌ๋ ค์ฃผ์‹  ๊ธ€์„ ๋ณด๊ณ  ์ด๋Ÿฐ ๋ฌธ์ œ์˜€์ง€ ํ•˜๊ณ  ์‰ฝ๊ฒŒ ๊ธฐ์–ต์ด ๋‚ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ๊ธ€์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ +,-,*,/๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. java ์ฝ”๋“œ์ธ๋ฐ C++์ด๋ž‘ ๋น„์Šทํ•œ ๋ฌธ๋ฒ•์ด ๋งŽ์•„์„œ ์ดํ•ดํ•˜์‹œ๋Š”๋ฐ ํฐ ์–ด๋ ค์›€์€ ์—†์„๊ฑฐ ๊ฐ™์•„์š”. ์ œ ์ƒ๊ฐ์—๋Š” ์ด๊ฒŒ ๋” ์ง๊ด€์ ์ด๊ธฐ๋Š” ํ•œ ์ฝ”๋“œ ๊ฐ™์•„์„œ ๋“ค๊ณ ์™€๋ดค์Šต๋‹ˆ๋‹ค๋งŒ.. C++์—์„œ๋„ ์ด๋Ÿฐ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ๊ตณ์ด? ์‹ถ์–ด์„œ ์•ˆํ•˜๋Š”๊ฑด์ง€ ์ž๋ฃŒ๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค๋”๋ผ๊ณ ์š”. ํ˜น์‹œ ์•„์‹œ๋Š” ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”!

์šฐ์„ ์ˆœ์œ„ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ œํ’€๊ธฐ

์•„๋ฌด๋ž˜๋„ ์ง๊ด€์„ฑ ๋•Œ๋ฌธ ์•„๋‹๊นŒ์š”..? ์ฝ์–ด๋ณด๋‹ˆ ์ € ๋ฐฉ๋ฒ•์„ ์“ฐ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

@YIM2UL2ET YIM2UL2ET merged commit 8170841 into main Mar 29, 2024
@YIM2UL2ET YIM2UL2ET deleted the 11-YIM2UL2ET branch March 29, 2024 13:27
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.

4 participants