-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackbracket.cpp
80 lines (73 loc) · 1.18 KB
/
stackbracket.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
class stack{
public:
char arr[100000];
int pos[100000];
int top=-1;
void pop(){
if(top==-1){
cout<<"EMPTY"<<endl;
}
else{
top=top-1;
}
}
char top1(){
return arr[top];
}
int topp(){
return pos[top];
}
void push(char a,int b){
top++;
arr[top]=a;
pos[top]=b;
}
int size(){
return top+1;
}
bool isempty(){
if(top==-1){return true;
}
else{
return false;}
}
void display(){
for(int i=top;i>=0;i++){
cout<<arr[i]<<endl;
}
}
}st;
int main(){
string s;
getline(cin,s);
int t=0;
for(int i=0;i<s.length();i++){
if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
st.push(s[i],i+1);
}
else if(s[i]==')' || s[i]=='}'|| s[i]==']'){
if(st.isempty()){
cout<<i+1;
t=1;
break;
}
else if((s[i]=='}'&& st.top1()=='{') ||(s[i]==']'&& st.top1()=='[' )|| (s[i]==')'&& st.top1()=='(') ){
st.pop();
}
else{t=1;
cout<<i+1;
break;
}
}
}
if(t==0 && st.isempty()){
cout<<"Success";
}
else if(t==0 && !st.isempty()){
cout<<st.topp();
}
system("pause");
return 0;
}