-
Notifications
You must be signed in to change notification settings - Fork 0
/
141.cpp
64 lines (55 loc) · 1 KB
/
141.cpp
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
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <stack>
char obter_parentese_oposto(char parentese)
{
char oposto;
switch (parentese)
{
case ')':
oposto = '(';
break;
case ']':
oposto = '[';
break;
case '}':
oposto = '{';
break;
default:
oposto = '(';
break;
}
return oposto;
}
int main()
{
// Desvincula a E/S entre C++ e C
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string line;
while (std::getline(std::cin, line))
{
bool balanced = true;
std::stack<char> stack;
for (char c : line)
{
if (c == '(' || c == '[' || c == '{')
stack.push(c);
else if (c == ')' || c == ']' || c == '}')
{
if (!stack.empty() && stack.top() == obter_parentese_oposto(c))
stack.pop();
else
{
balanced = false;
break;
}
}
}
if (balanced && stack.empty())
std::cout << "YES\n";
else
std::cout << "NO\n";
}
return 0;
}