-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestValidParentheses.cpp
More file actions
executable file
·49 lines (42 loc) · 1.04 KB
/
LongestValidParentheses.cpp
File metadata and controls
executable file
·49 lines (42 loc) · 1.04 KB
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
//
// LongestValidParentheses.cpp
// leetcode
//
// Created by witwolf on 5/6/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <string>
#include <stack>
#include <iostream>
#include <utility>
using namespace std;
class Solution {
public:
int longestValidParentheses(string s) {
int maxLength = 0;
int length = 0;
stack<pair<char,int> > stk;
stk.push(make_pair('#', -1));
for(int i = 0 ; i < s.length() ; ++i){
pair<char,int> top = stk.top();
if(s[i] - top.first == 1){
stk.pop();
length = i - stk.top().second;
if(length > maxLength){
maxLength = length;
}
}else{
stk.push(make_pair(s[i],i));
}
}
return maxLength;
}
};
int main(int argc,char **argv){
Solution s;
while(true){
string ss;
cin >> ss;
cout << ss << ":" << s.longestValidParentheses(ss) << endl;
}
}