class Solution {
public:
/**
*
* @param s string字符串
* @return int整型
*/
int longestValidParentheses(string s) {
// write code here
int len = s.size();
vector<int> dp(len,0);
int res = 0;
for(int i=1;i<len;i++){
if(s[i] == ')'){
int pre = i - dp[i-1] - 1;
if(pre >= 0 && s[pre] == '('){
pre--;
int p = 0;
if(pre >= 0) p = dp[pre];
dp[i] = dp[i-1] + 2 + p,res = max(res,dp[i]);
}
}
}
return res;
}
};