-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_valid_expr.c
114 lines (99 loc) · 1.88 KB
/
longest_valid_expr.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
typedef struct node {
struct node *next;
int val;
} node;
typedef struct stack {
struct node *top;
int len;
} stack;
void push(stack **s, node *n) {
if (!*s || !s)
return;
n->next = (*s)->top;
(*s)->top = n;
(*s)->len++;
}
node* pop(stack **s)
{
node *n = (*s)->top;
(*s)->top = n->next;
(*s)->len--;
return n;
}
void pushint(stack **s, int i) {
node *n = malloc(sizeof(node));
if (n) {
n->val = i;
n->next = NULL;
push(s, n);
}
}
int popint(stack **s) {
node *n;
int val = -1;
if ((*s)->top)
{
n = pop(s);
val = n->val;
free(n);
}
return val;
}
int peek(stack *s) {
return (s)->top->val;
}
bool stackempty(stack *s) {
return (s)->top == NULL;
}
int main() {
stack *s;
//char *expr = "())((())";
char *expr = "(((()))";
int i, len, val;
int max_len = INT_MIN;
s = malloc (sizeof (stack));
memset(s, 0, sizeof(stack));
len = strlen(expr);
for (i=0; i<len; i++) {
if (expr[i] == '(') {
pushint(&s, i);
} else {
val = popint(&s);
if (val == -1) {
pushint(&s, i);
} else {
max_len = max_len > (i-val)? max_len:(i-val);
}
}
}
printf("%d\n", max_len+1);
while(s->len)
(void) popint(&s);
}
#if 0
public class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(i);
} else {
stack.pop();
if (stack.empty()) {
stack.push(i);
} else {
maxans = Math.max(maxans, i - stack.peek());
}
}
}
return maxans;
}
}
#endif