개인공부/코딩테스트
백준 4949번 : 균형잡힌 세상 / C++
Itsumo
2023. 2. 26. 00:30
4949번: 균형잡힌 세상
각 문자열은 마지막 글자를 제외하고 영문 알파벳, 공백, 소괄호("( )"), 대괄호("[ ]")로 이루어져 있으며, 온점(".")으로 끝나고, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마지막에
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
while (true)
{
string str;
getline(cin, str);
if (str[0] == '.')
break;
stack<int> S;
bool yes = true;
for (int i = 0; i < str.size(); ++i)
{
if (str[i] == '(')
{
S.push(1);
}
else if (str[i] == ')')
{
if (S.empty() || S.top() != 1)
{
yes = false;
break;
}
S.pop();
}
else if (str[i] == '[')
{
S.push(2);
}
else if (str[i] == ']')
{
if (S.empty() || S.top() != 2 )
{
yes = false;
break;
}
S.pop();
}
}
if (yes == true && S.empty())
cout << "yes" << '\n';
else
cout << "no" << '\n';
}
return 0;
}
|
cs |