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

9 ljedd2 #156

Merged
merged 13 commits into from
Oct 17, 2024
Merged

9 ljedd2 #156

merged 13 commits into from
Oct 17, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 7, 2024

πŸ”— 문제 링크

λ°±μ€€ μŠ€νƒ | BOJ 2716 μ›μˆ­μ΄ 맀달기

문제 μ„€λͺ…
image

  • 덩꡴은 λ‘κ°œμ˜ 덩꡴둜 λ‚˜λˆ μ§ˆ 수 μžˆλŠ”λ° 덩꡴이 λŠμ–΄μ§€μ§€ μ•ŠκΈ° μœ„ν•΄μ„œλŠ” λ‚˜λˆ μ§„ 두 덩꡴은 같은 수의 μ›μˆ­μ΄λ“€μ΄ 맀달렀 μžˆμ–΄μ•Όν•œλ‹€. 덩꡴의 λ‚˜λˆ μ§€λŠ” 지점을 "[]"둜 μ •μ˜ 내리면 덩꡴의 ꡬ쑰λ₯Ό κΊΎμ‡ κ΄„ν˜Έλ‘œ ν‘œμ‹œν•  수 μžˆλ‹€. λ˜ν•œ 덩꡴의 κΉŠμ΄λŠ” 25λ₯Ό λ„˜μ§€ μ•ŠλŠ”λ‹€.
  • μ›μˆ­μ΄λ“€μ€ 덩꡴의 κ· ν˜•μ„ μœ μ§€ν•˜λ©΄μ„œ λ‚˜λ¬΄κΌ­λŒ€κΈ°μ— 도달할 수 μžˆλŠ” μ΅œμ†Œ μ›μˆ­μ΄ 수λ₯Ό μ•Œκ³  μ‹Άλ‹€. λ‚˜λ¬΄ κΌ­λŒ€κΈ°μ— λ„λ‹¬ν•˜κΈ° μœ„ν•΄μ„œ μ΅œμ†Œ ν•œ 마리 μ›μˆ­μ΄κ°€ ν•„μš”ν•˜λ‹€.

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ λ‚΄μ™Έ

✨ μˆ˜λ„ μ½”λ“œ

문제 읽기

  • μž…λ ₯은 였직 λŒ€κ΄„ν˜Έλ§Œ 주어진닀. λŒ€κ΄„ν˜Έκ°€ 있으면 덩꡴이 2개둜 λΆ„λ¦¬λ©λ‹ˆλ‹€.
  • 각 λ©κ΅΄μ—λŠ” λ™μΌν•œ 수의 μ›μˆ­μ΄κ°€ μžˆμŠ΅λ‹ˆλ‹€.
  • 덩꡴은 2개둜만 λ‚˜λ‰©λ‹ˆλ‹€.
    image

문제의 그림을 μ‚΄νŽ΄λ³΄λ©΄ 첫 번째 κ°ˆλΌμ§„ λ©κ΅΄μ—μ„œλŠ” μ›μˆ­μ΄κ°€ 4λ§ˆλ¦¬κ°€ 맀달릴 수 있고 (2+2 , 1+1+2)
κ°ˆλΌμ§„ 였λ₯Έμͺ½ νŠΈλ¦¬μ—μ„œλŠ” 2λ§ˆλ¦¬μ”© 맀달릴 수 μžˆμŠ΅λ‹ˆλ‹€. (1+1, 2)

λ”°λΌμ„œ, 덩꡴은 무쑰건 2κ°œμ”©μœΌλ‘œ λ‚˜λˆ μ Έ ν•œ 덩꡴의 depthκ°€ κΉŠμ–΄μ§ˆμˆ˜λ‘ λ‹€λ₯Έ 덩꡴도 λŠ˜μ–΄λ‚œ μˆ˜μ— 맞게 μ›μˆ­μ΄ μˆ˜κ°€ μ¦κ°€ν•©λ‹ˆλ‹€

μ΅œλŒ€ κΉŠμ΄κ°€ 3이고 이 λ•Œ ν•„μš”ν•œ μ›μˆ­μ΄λŠ” 8λ§ˆλ¦¬κ°€ λ˜μ–΄ 2**3 = 8이 λ©λ‹ˆλ‹€.

image

차수λ₯Ό depth둜 ν•˜λŠ” 2의 μ œκ³±μˆ˜κ°€ 정닡이 되며, λŒ€κ΄„ν˜Έμ˜ λ°©ν–₯에 따라 깊이λ₯Ό μΈ‘μ •ν•˜λŠ” 것이 문제 ν•΄κ²°μ˜ ν•΅μ‹¬μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

λŒ€κ΄„ν˜Έ μ—¬λŠ” λΆ€λΆ„(' [ ')이 λ“€μ–΄μ˜€λ©΄ 깊이λ₯Ό μ¦κ°€μ‹œν‚€κ³ , μ•„λ‹Œ κ²½μš°μ—λŠ” 깊이λ₯Ό 쀄여 μ€λ‹ˆλ‹€. μŠ€νƒμ„ ꡳ이 ν™œμš©ν•˜μ§€ μ•Šμ•„λ„ 파이썬의 replace()λ₯Ό μ‚¬μš©ν•˜μ—¬ 같은 레벨의 []κ°€ μžˆλ‹€λ©΄ κ³„μ†ν•΄μ„œ μ œκ±°ν•΄μ£Όλ©΄μ„œ κΉŠμ΄κ°€ μ΅œλŒ€κ°€ λ˜λŠ” 지점을 찾을 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ !

μ΅œμ’…μ½”λ“œ

Stack으둜 μ—¬λŠ” κ΄„ν˜Έμ˜ κ°œμˆ˜μ— 따라 깊이 λΉ„κ΅ν•˜κΈ°

import sys

input = sys.stdin.readline

for _ in range(int(input())):
    s = input().rstrip()  # κ΄„ν˜Έ λ¬Έμžμ—΄

    max_depth = 0  # μ΅œλŒ€ 깊이λ₯Ό μ €μž₯ν•  λ³€μˆ˜
    tree = list()  # ν˜„μž¬ μ—΄λ¦° κ΄„ν˜Έλ₯Ό μ €μž₯ν•  μŠ€νƒμž„

    for i in s:
        # μ—¬λŠ” κ΄„ν˜ΈμΌ 경우 μŠ€νƒμ— μ—΄λ¦° κ΄„ν˜Έ μΆ”κ°€
        if i == '[':
            tree.append('[')
            continue
        # λ‹«λŠ” κ΄„ν˜ΈμΌ 경우
        max_depth = max(len(tree), max_depth)
        # ν˜„μž¬ κΉŠμ΄μ™€ μ΅œλŒ€ 깊이 쀑 큰 값을 μ €μž₯ν•˜κ³ 
        tree.pop()  # λ§ˆμ§€λ§‰ μ—΄λ¦° κ΄„ν˜Έλ₯Ό 제거

    print(2 ** max_depth)  # μŒμ„ μ΄λ£°λ•Œ 가지 생성, 가지2-.1κ°œμ”© λ‚˜λˆ κ°€μ§
    # κ²°κ΅­μ—” 2의 depth승

replace()λ₯Ό ν™œμš©ν•œ μ’€ 더 κ°„λ‹¨ν•œ μ½”λ“œ

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    cnt = 0
    s = input().rstrip()
    while "[]" in s:
        s = s.replace("[]", "")
        cnt += 1

    print(2**cnt)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@jung0115
Copy link
Member

jung0115 commented Sep 9, 2024

μŠ€νƒ 문제λ₯Ό μ˜€λžœλ§Œμ— λ³΄λŠ” 것 κ°™λ‹€κ³  μƒκ°ν•˜λ©΄μ„œ 정은 λ‹˜μ˜ 풀이λ₯Ό λ΄€λŠ”λ°, replaceλ₯Ό μ΄μš©ν•œ 방법은 생각지도 λͺ»ν–ˆλ„€μš” 😳

replaceλ₯Ό ν™œμš©ν•œ μ½”λ“œμ—μ„œ whileλ¬Έ 반볡 쑰건을 while "[]" in s: λŒ€μ‹ , while len(s) > 0:으둜 해도 λ˜‘κ°™μ΄ λ™μž‘ν•©λ‹ˆλ‹€! 그리고 쑰건을 μ΄λ ‡κ²Œ λ°”κΎΈλ©΄ μ†λ„λ©΄μ—μ„œλ„ λ―Έμ„Έν•˜μ§€λ§Œ μ’€ 더 λΉ¨λΌμ§€λŠ” 것 κ°™μ•„μš”

이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

저도 문제λ₯Ό 보고 μŠ€νƒμ„ μ΄μš©ν•΄μ„œ ν’€μ–΄μ•Όκ² λ‹€κ³  μƒκ°ν•΄μ„œ λ°”λ‘œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€!

import sys

count = int(sys.stdin.readline().strip())

for _ in range(count):
    sequence = sys.stdin.readline().strip()

    # μ΅œλŒ€ 깊이
    max_depth = 0  
    # ν˜„μž¬ κ΄„ν˜Έ μŠ€νƒ
    stack = []  

    # μž…λ ₯된 κ΄„ν˜Έ λ¬Έμžμ—΄ 처리
    for bracket in sequence:
        # λ‹«λŠ” κ΄„ν˜ΈμΈ 경우
        if bracket == ']':
            # μŠ€νƒ 길이λ₯Ό 기반으둜 깊이 κ°±μ‹ 
            max_depth = max(len(stack), max_depth) 
            # μŠ€νƒμ—μ„œ μ—¬λŠ” κ΄„ν˜Έ 제거
            stack.pop()
        else:
            # μ—¬λŠ” κ΄„ν˜Έλ₯Ό μŠ€νƒμ— μΆ”κ°€
            stack.append('[')

    # κ°€μž₯ κΉŠμ€ κ³³μ—μ„œμ˜ 2의 제곱 좜λ ₯
    print(2 ** max_depth)

μ •μ€λ‹˜μ²˜λŸΌ replaceλ₯Ό ν™œμš©ν•΄μ„œ μ½”λ“œλ₯Ό μž‘μ„±ν•  μˆ˜λ„ μžˆκ΅°μš”!
replace() ν•¨μˆ˜λŠ” μ²˜μŒλ³΄λŠ”λ° 덕뢄에 μƒˆλ‘œμš΄ ν•¨μˆ˜λ₯Ό ν•˜λ‚˜ μ•Œμ•„κ°‘λ‹ˆλ‹€πŸ˜ κ°μ‚¬ν•΄μš©πŸ˜πŸ˜

Copy link
Collaborator

@wonjunYou wonjunYou left a comment

Choose a reason for hiding this comment

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

μ •μ€λ‹˜μ˜ μΉœμ ˆν•œ κ·Έλ¦Ό μ„€λͺ… 덕뢄에 κΉ”λ”ν•˜κ²Œ μ •λ¦¬λ˜μ—ˆλ„€μš”! 저도 stack을 μ΄μš©ν•΄μ„œ ν’€μ—ˆκ³ , replace()처럼 λ¬Έμžμ—΄λ‘œ μ ‘κ·Όν•΄μ„œ ν’€μ–΄λ‚Ό 생각은 λͺ» ν–ˆλŠ”λ° ν•˜λ‚˜ λ°°μ›Œκ°‘λ‹ˆλ‹€ :)

고생 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€!!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            int maxCount = 0;
            Stack<Character> stack = new Stack<>();
            String s = br.readLine();

            for (int j = 0; j < s.length(); j++) {
                char letter = s.charAt(j);

                if (letter == '[') {
                    stack.push(letter);
                    maxCount = Math.max(maxCount, stack.size());
                } else if (letter == ']') {
                    stack.pop();
                }
            }
            System.out.println((int) Math.pow(2, maxCount));
        }

    }

}

@LJEDD2 LJEDD2 deleted the 9-ljedd2_ branch October 17, 2024 14:25
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