forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AC_stack_n.cpp
47 lines (40 loc) · 1.12 KB
/
AC_stack_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n.cpp
* Create Date: 2015-01-26 10:25:16
* Descripton: Using stack
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> lefts;
int max_len = 0, match_pos = -1; // position of first
// matching '(' - 1
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(')
lefts.push(i);
else {
if (lefts.empty()) // no matching left
match_pos = i;
else { // match a left
lefts.pop();
if (lefts.empty())
max_len = max(max_len, i - match_pos);
else
max_len = max(max_len, i - lefts.top());
}
}
}
return max_len;
}
};
int main() {
string test;
Solution s;
while (cin >> test)
cout << s.longestValidParentheses(test) << endl;
return 0;
}