-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthese.cpp
More file actions
61 lines (53 loc) · 1.37 KB
/
Parenthese.cpp
File metadata and controls
61 lines (53 loc) · 1.37 KB
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
/*
Problem: Parentheses
Description
Given a string containing only characters (, ), [, ] {, }. Write a program that check whether the string is correct in expression.
Example:
([]{()}()[]): correct
([]{()]()[]): incorrect
Input
One line contains the string (the length of the string is less than or equal to 10^6)
Output
Write 1 if the sequence is correct, and write 0, otherwise
Example:
input
(()[][]{}){}{}[][]({[]()})
output
1
*/
#include<bits/stdc++.h>
using namespace std;
bool isPair(char s_i, char top_stack) {
if (s_i == '}' && top_stack=='{') return true;
if (s_i == ']' && top_stack=='[') return true;
if (s_i == ')' && top_stack=='(') return true;
return false;
}
bool solve(string seriesOfParentheses) {
stack<char> s;
for (int i = 0; i < seriesOfParentheses.size(); i++)
{
if (seriesOfParentheses[i] == '(' || seriesOfParentheses[i] == '{' || seriesOfParentheses[i] == '[')
{
s.push(seriesOfParentheses[i]);
}
else {
if (s.empty() || (!isPair(seriesOfParentheses[i], s.top())))
{
return false;
}
s.pop();
}
}
if (!s.empty())
{
return false;
}
return true;
}
int main() {
string seriesOfParentheses;
cin >> seriesOfParentheses;
cout << solve(seriesOfParentheses) << endl;
return 0;
}