Longest Valid Parenthesesmy SubmissionsQuestionSolutionTotal accepted:47520 Total submissions:222865 difficulty:hard
Given A string containing just the characters ‘(‘
‘)‘
and, find the length of the longest valid (well-formed) parenthe SES substring.
"(()"
for, the longest valid parentheses substring "()"
are, which has length = 2.
Another example ")()())"
is, where the longest valid parentheses substring "()()"
are, which has length = 4.
#include <iostream>#include<string>using namespacestd;#defineNUM 350classSolution { Public: intLongestvalidparentheses (strings) {intLen =s.length (); if(Len <2)return 0; //int**dp= (int * *) new int[10000][10000]; //int** isvalid= (int * *) new int[10000][10000]; //int max = 0; //memset (DP, 0, 10000 * 10000 * sizeof (int)); //memset (isValid, 0, 10000 * 10000 * sizeof (int)); intDp[num][num]; intIsvalid[num][num]; intMax =0; Memset (DP,0, num * num *sizeof(int));//will affect the output of the resultmemset (IsValid,0, num * num *sizeof(int)); for(inti =1; I < s.length (); ++i) { for(intj = i-1; J >=0; --j) {if(S[j] ='('&&s[i] = =')')//situation one { inttemp =0; for(intK = j +1; K < I; ++k) {if(Isvalid[j][k] && isvalid[k +1][i]) Temp=1; } if(i = = j +1|| Dp[j +1][i-1] ||temp) {Isvalid[j][i]=1; Dp[j][i]= I-j +1; Max= max > Dp[j][i]?Max:dp[j][i]; } Else{Isvalid[j][i]=0; Dp[j][i]= Dp[j +1][i] > Dp[j][i-1] ? Dp[j +1][i]: dp[j][i-1]; } } Else if(S[j] = ='('&&s[i] = ='(')//Scenario Two{Isvalid[j][i]=0; Dp[j][i]= Dp[j][i-1]; } Else if(S[j] = =')'&&s[i] = =')')//situation three{Isvalid[j][i]=0; Dp[j][i]= Dp[j +1][i]; } Else//Scenario Four{Isvalid[j][i]=0; Dp[j][i]= Dp[j +1][i-1]; } } } returnMax; }};intMain () {solution test; stringS1 =")(())()"; intres =test.longestvalidparentheses (S1); cout<< Res <<Endl; return 0;}
Helpless, had to search for help the great God, DP:
This problem can be solved with one-dimensional dynamic programming in inverse way. Suppose the input parenthesis expression is string s, maintaining a one-dimensional array of length s.length () dp[], and array elements initialized to 0. Dp[i] represents the longest valid matching brace substring length from s[i] to s[s.length-1]. The following relationship exists:
Dp[s.length-1] = 0; reverse dp[] from i-2 to 0, and record its maximum value.
If s[i] = = ' (', then the value of s[i] is calculated from I to s.length-1 in S. This calculation is divided into two steps, through Dp[i + 1] (Note that dp[i + 1] has been solved in the previous step):
In S, look for a valid brace matching substring length starting from i + 1, i.e. Dp[i + 1], skipping this valid brace substring, and looking at the next character, labeled J = i + 1 + dp[i + 1]. If J does not cross over and s[j] = = ') ', then S[i ... j] is a valid brace match, dp[i] =dp[i + 1] + 2.
After a valid match length of s[i ... j] is obtained, if J + 1 does not cross the bounds, then the value of Dp[i] is added to the longest valid match starting with J + 1, i.e. Dp[j + 1].
O (N)
1 intLongestvalidparentheses (strings) {2 //Note:the Solution Object is instantiated only once.3 intSlen =s.length ();4 if(slen<2)return 0;5 intMax =0;6 int* DP =New int[Slen];7Memset (DP,0,sizeof(int)*slen);8 9 for(inti=slen-2; i>=0; i--)Ten { One if(s[i]=='(') A { - intj = i+1+dp[i+1]; - if(J<slen && s[j]==')') the { -dp[i]=dp[i+1]+2; - intK =0; - if(j+1<slen) k=dp[j+1]; +Dp[i] + =K; - } +max = Max>dp[i]?Max:dp[i]; A } at } - Delete[] DP; - returnMax; -}
This piece of code is really clever!!
Often see their own this paragraph of the bitterness of the Code