Longest Valid Parentheses
class Solution {
public:
int longestValidParentheses(string s) {
const int len = s.length();
stack<int> stk;
vector<bool> v(len, false);
for(int i = 0; i < len; i++){
if(s[i] == '('){
stk.push(i);
}else{
if(stk.empty()){
continue;
}else{
v[stk.top()] = true;
stk.pop();
v[i] = true;
}
}
}
int maxlen = 0, cur = 0;
for(int i = 0; i < len; i++){
if(v[i]){
cur++;
}else{
cur = 0;
}
maxlen = cur > maxlen?cur : maxlen;
}
return maxlen;
}
};