-
Notifications
You must be signed in to change notification settings - Fork 1
7-hadongun #25
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
7-hadongun #25
Conversation
dohyeondol1
left a comment
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.
λ€μλ§λ λ¬Έμμ΄ μ€ν λ¬Έμ λ€μ
μ λ²μ κ΄νΈλ¬Έμ λ₯Ό νμλ κΈ°μ΅μ΄ μλλ°, λΉμ·ν μ νμΈ κ² κ°μ΅λλ€.
μ λ μ²μμ '.'κΉμ§ λ¬Έμ μ
λ ₯μ λ°μΌλ €κ³ νλλ°,
μκ°ν΄λ³΄λ μ
λ ₯μμ μν°λ₯Ό μΉκ²λλ©΄ κ²°κ΅ κ°ν λ¬Έμ λν μ
λ ₯μ΄ λκΈ°λλ¬Έμ
stringμ μ΄μ©ν΄μ κ·Έλ₯ μ€ μ μ²΄λ§ λ°μμ΅λλ€.
pythonμλ λΉμ·ν κΈ°λ₯μ΄ μμ κ² κ°μλ° νλ² μμ보μ
λ μ’μ κ² κ°μ΅λλ€.
νμ΄ λ°©μ μ체λ λκ°μ΅λλ€.
C++ μ½λ
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
string str;
while(true) {
getline(cin, str);
if(str == ".") break;
stack<char> stack;
bool balance = true;
for(char c : str) {
if(c == '(' || c == '[') stack.push(c);
else if (c == ')') {
if(stack.empty() || stack.top() != '(') {
balance = false;
break;
}
stack.pop();
}
else if(c == ']') {
if(stack.empty() || stack.top() != '[') {
balance = false;
break;
}
stack.pop();
}
}
if(!stack.empty()) balance = false;
cout << (balance ? "yes" : "no") << '\n';
}
return 0;
}λ§μ°¬κ°μ§λ‘ μλ μ½λλ₯Ό μμ±νμλ κ±Έ μΆμ²λ립λλ€!
μ€λͺ
μ μ μ¨μ£Όμ
¨μ§λ§, μλ μ½λλ₯Ό μ°λκ² μ€ν°λμ λΏλ§ μλλΌ μμ μκ²λ λμμ΄ λκ±°λ©μ.
μ€ν λ¬Έμ μ λν΄μ μ μ κ°μ μ‘μκ°μλ κ² κ°μμ 보기 μ’μ΅λλ€!!
λν μ΄μ μ λΉν΄μ μ μ μ½λκ° κΉλν΄μ§λ κ² κ°μμ!!
|
μ λ μ²μμλ μ€νμ μ μ°κ³ μΆμ΄μ κ΄νΈμ κ°μλ‘ κ³μ°νλ λ°©λ²μ λ μ¬λ Έλλ°, νλ €μ μ€νμΌλ‘ λμμμ΅λλ€.. μ€μλμ΄ νμλ λ¬Έμ μ λ§€μ° μ μ¬νκ΅°μ γ γ Python
import sys
input = sys.stdin.readline
while True:
line = input().rstrip('\n')
if line == '.':
break
stack = []
balanced = True
for char in line:
if char == '(' or char == '[':
stack.append(char)
elif char == ')':
if not stack or stack.pop() != '(':
balanced = False
break
elif char == ']':
if not stack or stack.pop() != '[':
balanced = False
break
if stack:
balanced = False
print("yes" if balanced else "no") |
caucsejunseo
left a comment
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.
μ κ° μ΄μ μ νμλ λ¬Έμ μ μ μ¬ν΄μ μ½κ² νμ΄λ₯Ό λ μ¬λ Έλ κ² κ°μ΅λλ€!
μ΄λ»κ² μ½λλ₯Ό μμ±ν΄μΌ '.'μ μ λ ₯λ°μμ λ μ λ ₯μ κ·Έλ§λ°κ² ν μ μμμ§ κ³ λ―Όνμλλ° μ μκ°μ΄ μλλλΌκ΅¬μ
while (1) { fgets(str, sizeof(str), stdin); // ν μ€ μ
λ ₯ λ°κΈ° if (strcmp(str, ".\n") == 0) break; // μ
λ ₯ μ’
λ£ μ‘°κ±΄
λ°±μ€μμλ κ·Έλ₯ κΌ μ¬μ©μκ° λ§μ§λ§μ '.'μ μ λ ₯νμ§ μμλ λλ μ½λλ₯Ό μμ±νλ κ²λ νλμ λ°©λ²μ΄λΌλ κ±Έ μκ² λμλ€μ :)
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/4949
βοΈ μμλ μκ°
1.5h
β¨ μλ μ½λ
μ€μλμ λ¬Έμμ΄ λ¬Έμ λ₯Ό λ³΄κ³ νλ¦νμ λ¬Έμμ΄, μ€ν λ¬Έμ λ₯Ό κ°μ Έμλ΄€μ΅λλ΅
μλμ μ£ΌμμΌλ‘ μ½μ§ν μ½λλ₯Ό λ£μ΄λλλ° λ¬Έμ 쑰건μ μ λλ‘ μ λ³΄κ³ νμμ΄μ.
μ£ΌμμΌλ‘ λ¬λ¦° μ½λλ₯Ό 보면 κ΄νΈμ κ°μλ§ μΈμ΄μ κ· νμ μ΄λ£¨λ©΄ λλλ‘ μ½λλ₯Ό μμ± + stackμ μ ν νμ©νμ§ λͺ»νλ μ½λ
κ°κ°μ μ€μ ν λ²μ© μ λ ₯ λ°κ³ yes or noλ₯Ό μΆλ ₯ ν λ€μ λ¬Έμ₯μ λ€μ μ λ ₯ λ°λ ꡬ쑰μ λ¬Έμ μ΄κ³ 맨 λ§μ§λ§μ μ νλκ° μ λ ₯λκΈ° λλ¬Έμ μ νλλ₯Ό μ λ ₯λ°μΌλ©΄ μ 체 whileλ¬Έμ μ’ λ£μν€λλ‘ ifλ¬Έμ μ€κ³νμ΅λλ€
μ΄ν charrrμ 곡백 λ¬Έμλ₯Ό μ κ±°νλ©΄μ ν μ€μ μ λ ₯ λ°κ³ forλ¬Έμ ν΅ν΄ ν λ¬Έμμ© νμΈνμ΅λλ€.
κ΄νΈμ κ°μλ§ κ· νμ μ΄λ£¬λ€λ©΄ ") i have a apple (" μ΄λ¬ν λ¬Έμ₯μ΄λ μ±λ¦½μ΄ λ©λλ€.
λ°λΌμ μ¬λ κ΄νΈ, λκ΄νΈλ₯Ό λ°μΌλ©΄ μ΄λ₯Ό μ€νμ λ£μ΅λλ€. μ΄ν λ«λ κ΄νΈ, λκ΄νΈλ₯Ό λ°μ κ²½μ° stack λ΄μ μ¬λ κ΄νΈ, λκ΄νΈκ° μλ€λ©΄ μ΄λ κ· νμ μ΄λ£¬ κ²μΌλ‘ κ°μ£Όνκ³ μ€ν λ΄μ μλ μ¬λ κ΄νΈ, λκ΄νΈλ₯Ό μ§μλλ€.
λ§μ½ μ€ν λ΄μ μ무κ²λ μκ±°λ, μ¬λ κ΄νΈμΈλ°, λ«λ λκ΄νΈκ° λ€μ΄μλ€λ©΄ κ· νμ μ΄λ£¨μ§ λͺ»νλ―λ‘ balance = False λ‘ ν λΉ ν breakλ‘ λ°λ³΅λ¬Έμ νμΆν ν noλ₯Ό μΆλ ₯ν©λλ€
λͺ¨λ κ° λ¬Έμλ€μ λΉκ΅νμμ λ κ· νμ΄ μ‘νμμλ€λ©΄ balance μ κ°μ Trueμ΄κ³ , stack λ΄μλ μ무κ²λ λ¨μ§ μκ² λ©λλ€.
λ°λΌμ μ΄ κ²½μ° yesλ₯Ό μΆλ ₯ν©λλ€.
π μλ‘κ² μκ²λ λ΄μ©