-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path743.cpp
133 lines (112 loc) · 2 KB
/
743.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 66981
* Submitted at: 2018-10-16 18:35:18
*
* User ID: 539
* Username: 55211931
* Problem ID: 743
* Problem Name: Syntax Checker
*/
#include <iostream>
#include <string.h>
using namespace std;
class StackArray {
private:
int top = -1;
int capacity = 100000;
char stack[100000];
int num[100000];
public:
void Push(char x, int y);
void Pop();
bool IsEmpty();
char getTop();
int getTopNum();
};
void StackArray::Push(char x, int y) {
num[++top] = y;
stack[top] = x;
}
void StackArray::Pop() {
top--;
}
bool StackArray::IsEmpty() {
if (top == -1) {
return true;
}
else {
return false;
}
return (top == -1);
}
char StackArray::getTop() {
return stack[top];
}
int StackArray::getTopNum()
{
return num[top];
}
int main()
{
char s[100000];
while (cin.getline(s,100000))
{
StackArray c = StackArray() ;
for (int i = 0;i < strlen(s);i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
{
c.Push(s[i], i);
}
if (s[i] == ')' || s[i] == ']' || s[i] == '}')
{
if (s[i] == ')' && c.getTop() == '(')
{
c.Pop();
}
else if (s[i] == ']' && c.getTop() == '[')
{
c.Pop();
}
else if (s[i] == '}' && c.getTop() == '{')
{
c.Pop();
}
else
{
c.Push(s[i], i);
}
}
}
if (c.IsEmpty())
{
cout << "Success" << endl;
}
else
{
int close = -1, open = -1;
while (!c.IsEmpty())
{
if (c.getTop() == '(' || c.getTop() == '{' || c.getTop() == '[')
{
open = c.getTopNum()+1;
}
if (c.getTop() == ')' || c.getTop() == '}' || c.getTop() == ']')
{
close = c.getTopNum()+1;
}
c.Pop();
}
if (close != -1)
{
cout << close << endl;
}
else
{
cout << open << endl;
}
}
}
return 0;
}