-
Notifications
You must be signed in to change notification settings - Fork 1
/
P1944.c
75 lines (66 loc) · 1.51 KB
/
P1944.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 1000006
#define is_left(c) (c == '(' || c == '[')
#define is_right(c) (c == ')' || c == ']')
static char Pair(char c) {
if (c == ')') return '(';
else return '[';
}
char *str;
int stack[maxn];
char ans[maxn];
static int top = -1;
static int Empty() {
return top == -1;
}
static int Top() {
return Empty() ? -1 : stack[top];
}
static void Pop() {
/*if (!Empty())*/ top--;
}
static void Push(int i) {
stack[++top] = i;
}
static void Clear() {
top = -1;
}
int main() {
int len;
getline(&str, &len, stdin);
int start = 0, ans_start = 0, ans_len = 0;
char c;
for (int i = 0; i < len; i++) {
c = str[i];
if (c == '\n' || c == '\0') break;
if (is_left(c)) {
Push(i);
}
else {
if (!Empty() && str[Top()] == Pair(c)) {
Pop();
if (!Empty()) {
if (ans_len < i - Top()) {
ans_len = i - Top();
ans_start = Top() + 1;
}
}
else {
if (ans_len < i - start + 1) {
ans_len = i - start + 1;
ans_start = start;
}
}
}
else {
Clear();
start = i + 1;
}
}
}
strncpy(ans, &str[ans_start], ans_len);
printf("%s", ans);
exit(0);
}