-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
λ¬Έμμ΄ νμ΄
μ€ν λ¬Έμ λ₯Ό μ€λλ§μ 보λ κ² κ°λ€κ³ μκ°νλ©΄μ μ μ λμ νμ΄λ₯Ό λ΄€λλ°, replaceλ₯Ό μ΄μ©ν λ°©λ²μ μκ°μ§λ λͺ»νλ€μ π³ replaceλ₯Ό νμ©ν μ½λμμ whileλ¬Έ λ°λ³΅ 쑰건μ μ΄λ² μ°¨μλ μκ³ νμ ¨μ΅λλ€! |
There was a problem hiding this 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()
ν¨μλ μ²μ보λλ° λλΆμ μλ‘μ΄ ν¨μλ₯Ό νλ μμκ°λλ€π κ°μ¬ν΄μ©ππ
There was a problem hiding this 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));
}
}
}
π λ¬Έμ λ§ν¬
λ°±μ€ μ€ν | BOJ 2716 μμμ΄ λ§€λ¬κΈ°
λ¬Έμ μ€λͺ
βοΈ μμλ μκ°
1μκ° λ΄μΈ
β¨ μλ μ½λ
λ¬Έμ μ½κΈ°
λ¬Έμ μ κ·Έλ¦Όμ μ΄ν΄λ³΄λ©΄ 첫 λ²μ§Έ κ°λΌμ§ λ©κ΅΄μμλ μμμ΄κ° 4λ§λ¦¬κ° 맀λ¬λ¦΄ μ μκ³ (2+2 , 1+1+2)
κ°λΌμ§ μ€λ₯Έμͺ½ νΈλ¦¬μμλ 2λ§λ¦¬μ© 맀λ¬λ¦΄ μ μμ΅λλ€. (1+1, 2)
λ°λΌμ, λ©κ΅΄μ 무쑰건 2κ°μ©μΌλ‘ λλ μ Έ ν λ©κ΅΄μ depthκ° κΉμ΄μ§μλ‘ λ€λ₯Έ λ©κ΅΄λ λμ΄λ μμ λ§κ² μμμ΄ μκ° μ¦κ°ν©λλ€
μ΅λ κΉμ΄κ° 3μ΄κ³ μ΄ λ νμν μμμ΄λ 8λ§λ¦¬κ° λμ΄ 2**3 = 8μ΄ λ©λλ€.
μ°¨μλ₯Ό depthλ‘ νλ 2μ μ κ³±μκ° μ λ΅μ΄ λλ©°, λκ΄νΈμ λ°©ν₯μ λ°λΌ κΉμ΄λ₯Ό μΈ‘μ νλ κ²μ΄ λ¬Έμ ν΄κ²°μ ν΅μ¬μ΄μμ΅λλ€.
λκ΄νΈ μ¬λ λΆλΆ(' [ ')μ΄ λ€μ΄μ€λ©΄ κΉμ΄λ₯Ό μ¦κ°μν€κ³ , μλ κ²½μ°μλ κΉμ΄λ₯Ό μ€μ¬ μ€λλ€. μ€νμ κ΅³μ΄ νμ©νμ§ μμλ νμ΄μ¬μ
replace()
λ₯Ό μ¬μ©νμ¬ κ°μ λ 벨μ[]
κ° μλ€λ©΄ κ³μν΄μ μ κ±°ν΄μ£Όλ©΄μ κΉμ΄κ° μ΅λκ° λλ μ§μ μ μ°Ύμ μ μμμ΅λλ€ !μ΅μ’ μ½λ
StackμΌλ‘ μ¬λ κ΄νΈμ κ°μμ λ°λΌ κΉμ΄ λΉκ΅νκΈ°
replace()
λ₯Ό νμ©ν μ’ λ κ°λ¨ν μ½λπ μλ‘κ² μκ²λ λ΄μ©