Longest Valid parentheses

Source: Internet
Author: User

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.

The problem uses the dynamic programming thought

My first thought was to use a two-dimensional array to hold the length, and X, y of the two-dimensional array to be the string subscript, preserving the length of the substring between them.

But I can't find a simple formula to show the relationship between big and small problems, which is quite complicated, I've found 36 cases and I want to list them all, but there are 36 other things. As a result I gave up this method.

So I searched the internet for other people's algorithms, and found that only one-dimensional arrays can be solved

The value of this one-dimensional array means the longest valid matching brace substring length from s[i] to s[s.length-1] containing s[i]

To tell the truth, the meaning is not easy to imagine, and if I think about it, I first think of the longest valid matching bracket string that does not contain s[i]. is based on what kind of thinking to get such a result. Why can't I just think about it? I do not believe that it is my IQ problem, it must be that I do too few problems. Continue to brush the question, quantitative change will always have a qualitative change that day.

intLongestvalidparentheses (strings) {intLen =s.size (); if(Len <2)        return 0; intMaxLen =0; intF[len];  for(inti =0; i < Len; ++i)//initialized to 0 F[i]=0;  for(inti = len-2; I >=0; --i) {if(S[i] = ='(')//Since it contains s[i], then s[i] must be ' (' to form a matching bracket {intj = i +1+ f[i+1]; The next "Maybe" cannot match the position of the parenthesesif(J < len && S[j] = =')')//This "maybe" can be checked to match {F[i]= f[i+1] +2; if(j +1<len)//Beware if there is a matching brace in the back, the length is added f[i]+ = f[j+1]; }            if(F[i] >maxlen)//Maximum length maxlen=F[i]; }            }    returnMaxLen;}

Longest Valid parentheses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.