forked from rudi8848/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.1.brackets.cpp
More file actions
52 lines (48 loc) · 1013 Bytes
/
1.1.brackets.cpp
File metadata and controls
52 lines (48 loc) · 1013 Bytes
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
#include <iostream>
#include <string>
#include <stack>
#include <iterator>
struct iset
{
int index;
char ch;
};
int check_brackets(std::string const & str)
{
std::stack<struct iset> stack;
auto i = str.begin();
for (struct iset c; i < str.end(); ++i)
{
c.ch = *i;
c.index = i+1 - str.begin();
if (c.ch == '{' || c.ch == '(' || c.ch == '[')
stack.push(c);
else if (c.ch == '}' || c.ch == ')' || c.ch == ']')
{
if (stack.empty())
return ++i - str.begin();
if (c.ch == '}' && stack.top().ch != '{')
return ++i - str.begin();
else if (c.ch == ')' && stack.top().ch != '(')
return ++i - str.begin();
else if (c.ch == ']' && stack.top().ch != '[')
return ++i - str.begin();
else
stack.pop();
}
}
if (!stack.empty())
return stack.top().index;
return -1;
}
int main(void)
{
std::string str;
std::cin >> str;
int ret = 0;
if ((ret = check_brackets(str)) >= 0)
std::cout << ret << std::endl;
else
std::cout << "Success" << std::endl;
return 0;
}