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

17-suhyun113 #53

Merged
merged 2 commits into from
Oct 8, 2024
Merged

17-suhyun113 #53

merged 2 commits into from
Oct 8, 2024

Conversation

suhyun113
Copy link
Collaborator

@suhyun113 suhyun113 commented Sep 21, 2024

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

https://www.acmicpc.net/problem/1874

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

์•ฝ 1์‹œ๊ฐ„

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

๐Ÿ“Œ1. ๋ฌธ์ œ ์„ค๋ช…
์ฃผ์–ด์ง„ ์ˆ˜์—ด์„ ์Šคํƒ์„ ์‚ฌ์šฉํ•ด ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์–ด๋–ค push (+)์™€ pop(-) ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๐Ÿงํ•ต์‹ฌ : ์Šคํƒ์˜ ํ›„์ž…์„ ์ถœ ํŠน์„ฑ๊ณผ, ์ˆ˜๋ฅผ ๋„ฃ๋Š” ์ˆœ์„œ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์˜ค๋ฆ„์ฐจ์ˆœ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด

๐Ÿ“Œ2. ๋ฌธ์ œ ํ’€์ด ๋…ผ๋ฆฌ
๐Ÿš€1) ์ž…๋ ฅ ๋ฐ›๊ธฐ๐Ÿš€

  • ๋จผ์ €, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆ˜์—ด์„ sequence ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€2) ์Šคํƒ์— ์ˆ˜๋ฅผ pushํ•˜๋Š” ๊ณผ์ •๐Ÿš€

  • current ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šคํƒ์— ๋„ฃ์„ ์ˆซ์ž๋ฅผ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ฐจ๋ก€๋Œ€๋กœ ์Šคํƒ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • ์Šคํƒ์— ์ˆซ์ž๋ฅผ ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค result ๋ฆฌ์ŠคํŠธ์— +๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
  • while๋ฌธ์€ current๊ฐ€ num๊ณผ ๊ฐ™์•„์งˆ ๋•Œ๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ ์Šคํƒ์— ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ˆ˜๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

๐Ÿš€3) pop ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ๐Ÿš€

  • ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๊ฐ’์ด ์ˆ˜์—ด์˜ ๊ฐ’(num)๊ณผ ๊ฐ™๋‹ค๋ฉด, ์Šคํƒ์—์„œ ๊ทธ ๊ฐ’์„ ๊บผ๋ƒ…๋‹ˆ๋‹ค (pop).
  • ๊บผ๋‚ผ ๋•Œ๋งˆ๋‹ค result ๋ฆฌ์ŠคํŠธ์— -๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€4) ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ๐Ÿš€

  • ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๊ฐ’์ด ํ˜„์žฌ ์ˆ˜์—ด์˜ ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ๋” ์ด์ƒ ์ด ๊ฐ’์„ ๋งŒ๋“ค ๋ฐฉ๋ฒ•์ด ์—†์œผ๋ฏ€๋กœ ์ˆ˜์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ possible์„ False๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€5) ๊ฒฐ๊ณผ ์ถœ๋ ฅ๐Ÿš€

  • possible ๋ณ€์ˆ˜๊ฐ€ True๋ผ๋ฉด result ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ +์™€ - ์—ฐ์‚ฐ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • possible์ด False๋ผ๋ฉด ์ˆ˜์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ NO๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ3. ์ตœ์ข… ์ฝ”๋“œ

# 1874 : ์Šคํƒ ์ˆ˜์—ด

n = int(input())  # ์ˆ˜์—ด์˜ ํฌ๊ธฐ
sequence = [int(input()) for _ in range(n)]  # ์ฃผ์–ด์ง„ ์ˆ˜์—ด

stack = []
result = []
current = 1  # ์Šคํƒ์— ๋„ฃ์„ ์ˆซ์ž
possible = True

for num in sequence:
    # ํ˜„์žฌ ์ˆซ์ž๋ณด๋‹ค ์ˆ˜์—ด์—์„œ ํ•„์š”ํ•œ ์ˆซ์ž๊ฐ€ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ๊นŒ์ง€ push
    while current <= num:
        stack.append(current)
        result.append('+')
        current += 1

    # ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ์ด num๊ณผ ๊ฐ™๋‹ค๋ฉด pop
    if stack and stack[-1] == num:
        stack.pop()
        result.append('-')
    else:
        possible = False
        break

if possible:
    print("\n".join(result))
else:
    print("NO")

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

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

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

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

์˜ค๋žœ๋งŒ์— ์Šคํƒ ๋ฌธ์ œ ์ข‹์•„์š”...
ํ’€์ด ๊ณผ์ •์„ ์Šคํ… ๋ณ„๋กœ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ด ์ฃผ์…”์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๋˜ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘๐Ÿป
๊ฐœ์ธ์ ์œผ๋กœ ์žฌ๋ฏธ์žˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ ์ด๊ฑฐ ํ•œ๋ฒˆ ์–ด๋•Œ์š”?ใ…Žใ…Ž

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

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

๋จผ์ € ๋Šฆ์€ ๋ฆฌ๋ทฐ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ญ
์ €๋„ ์Šคํƒ ์ฒ˜์Œ ํ•  ๋•Œ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ, ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ์‰ฝ์ง€ ์•Š์•„์„œ ์•„์ง๋„ ๊ธฐ์–ต์— ๋‚จ๋Š” ๋ฌธ์ œ๋„ค์š”!
์ €๋Š” NO์ผ ๋•Œ ํ”Œ๋ž˜๊ทธ ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์„ ๋•Œ ๋ฐ”๋กœ ์ข…๋ฃŒํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์–ด์š”.
๋ฆฌ๋ทฐํ•˜๋‹ค ๋ณด๋‹ˆ, ์ด์ „์— ์ œ๊ฐ€ ํ’€์—ˆ๋˜ ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ๋Š๊ปด์ ธ์„œ ๋ฆฌํŒฉํ† ๋งํ•˜๊ฒŒ ๋์Šต๋‹ˆ๋‹ค. ๋•๋ถ„์— ๋” ๊น”๋”ํ•œ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™”๋„ค์š”.

๊น”๋”ํ•œ ํ’€์ด ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค๐Ÿ‘

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

int main()
{
    string buffer;
    stack<int> s;
    int n;
    cin >> n;
    int count = 0;
    
    while (n--)
    {
        int num;
        cin >> num;

        if (num > count)
        {
            while (num != count)
            {
                s.push(++count);
                buffer += "+\n";
            }
        }
        else if (num != s.top())
        {
            cout << "NO";
            return 0;
        }

        s.pop();
        buffer += "-\n";
    }
    
    cout << buffer;
    return 0;
}

Comment on lines +18 to +19
# ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ์ด num๊ณผ ๊ฐ™๋‹ค๋ฉด pop
if stack and stack[-1] == num:
Copy link
Member

Choose a reason for hiding this comment

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

stack[-1]์— ์ ‘๊ทผํ•  ๋•Œ ์Šคํƒ์ด ๋น„๋ฉด ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์œผ๋‹ˆ, ์ด๋Ÿฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์ข‹์€ ๋ฐฉ์‹์ด์ฃ !
๋‹ค๋งŒ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ์Šคํƒ์ด ๋น„๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†์–ด์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์—†์ด if stack[-1] == num:๋งŒ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

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