Recent Posts
Notice
No Rules Rules
균형잡힌 세상 (feat. 백준, 4949번) 본문
728x90
반응형
균형잡힌 세상
https://www.acmicpc.net/problem/4949
4949번: 균형잡힌 세상
하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
register int i;
string str;
while (true) {
stack<char> ans;
getline(cin, str);
if (str == ".")
break;
for (i = 0; i < str.size(); ++i) {
if (str.at(i) == '(')
ans.push('(');
else if (str.at(i) == '[')
ans.push('[');
else if (str.at(i) == ')') {
if (ans.empty() || ans.top() != '(')
break;
ans.pop();
}
else if (str.at(i) == ']') {
if (ans.empty() || ans.top() != '[')
break;
ans.pop();
}
}
if (i == str.size() && ans.empty())
cout << "yes";
else
cout << "no";
cout << "\n";
}
return 0;
}
// *&)*@*
- 자료구조 스택을 하나 생성합니다.
- '(' 또는 '['를 삽입합니다.
- ')' 의 문자를 입력받은 경우, 스택의 최근 문자가 '('가 아니라면 종료합니다.
- ']' 의 문자를 입력받은 경우, 스택의 최근 문자가 '['가 아니라면 종료합니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
오큰수 (feat. 백준, 17298번) (0) | 2022.08.12 |
---|---|
스택 수열 (feat. 백준, 1874번) (0) | 2022.08.12 |
괄호 (feat. 백준, 9012번) (0) | 2022.08.11 |
제로 (feat. 백준, 10773번) (0) | 2022.08.11 |
스택 (feat. 백준, 10828번) (0) | 2022.08.11 |
Comments