顿搜
飞过闲红千叶,夕岸在哪
类目归类
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int longestValidParentheses(char* s) {
int length = strlen(s);
if(length <= 1)
{
return 0;
}
int result = 0;
int* dp = (int *)malloc(length * sizeof(int));
dp[0] = 0;
for(int i = 1; i < length; i++)
{
if(s[i] ==')' && i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(')
{
dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
result = dp[i] > result ? dp[i] : result;
}else
{
dp[i] = 0;
}
}
return result;
}
int main()
{
char * s = ")()())";
printf("%d\n", longestValidParentheses(s));
system("pause");
return 0;
}