-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathB.cpp
57 lines (55 loc) · 1.24 KB
/
B.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
#include <fstream>
#include <string>
#include <stack>
std::ofstream out("brackets.out");
std::ifstream in("brackets.in");
int main()
{
std::stack <char> ans;
std::string s;
std::string answer = "YES";
in >> s;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '[' || s[i] == '{' || s[i] == '('){
ans.push(s[i]);
}
else
{
if (ans.size() == 0)
{
answer = "NO";
break;
}
else
{
if (s[i] == '}' && ans.top() == '{'){
answer = "YES";
ans.pop();
}
else if (s[i] == ']' && ans.top() == '['){
answer = "YES";
ans.pop();
}
else if (s[i] == ')'&& ans.top() == '('){
answer = "YES";
ans.pop();
}
else{
answer = "NO";
break;
}
}
}
}
if (ans.size() == 0)
{
out << answer;
}
else{
out << "NO";
}
out.close();
in.close();
return 0;
}