[Leetcode] 32. longest effective brackets

[Leetcode] 32. longest effective brackets

Keywords: DP, dynamic programming, dynamic rules.

In recent brush DP column topic, which is among a question.

Given that contains only '('and ')'string, to find the longest sub-string comprises an effective parentheses.

longest-valid-parentheses

Sample1

输入: "(()"
输出: 2
解释: 最长有效括号子串为 "()"

Sample2

输入: ")()())"
输出: 4
解释: 最长有效括号子串为 "()()"

For DP Well, above all, the need to abstract state function:

dp[i] 表示:以 S[i] 结尾的,最长有效括号串的长度。

Then the state transition equation:

如果 s.len == 0 or s.len == 1,那么返回 0 .
否则:
    if s[i]=='(' then dp[i]=0
    if s[i]==')' then:
        if s[i-1]='(' then dp[i] = dp[i-2] + 2 (数组下标是否越界,即 i>=2? )
        if s[i-1]=')' then:
            if s[i-dp[i-1]-1] == '(' then dp[i] = dp[i-dp[i-1]-2] + dp[i-1] + 2 (是否越界?)
            if s[i-dp[i-1]-1] == ')' then dp[i] = 0

The first ifstatement indicates that: the form ....(of such a string, is not necessarily valid.

The second ifstatement said: shaped like ....)such a string, so we need to consider s[i-1]:

  • s[i-1]='(': String shaped like ...(), obviously, correspond to the following index:

    i-2   i-1    i
     x     (     )       

    Obviously, dp[i]the value should be dp[i-2] + 2.

  • s[i-1]='): String shaped like ...)), obviously, correspond to the following index:

    ?          i-1    i
    x   (...    )     )

    Now consider s[i-1]the matching positions of the left parenthesis , s[i-1]=')'which is legitimate parentheses string length dp[i-1], the '('position should be:

    i - 1 - (dp[i-1] - 1) = i - dp[i-1]

    In other words, s[i] = )the matching left parenthesis position should be: i - dp[i-1] - 1.

    i-dp[i-1]-1  i-dp[i-1]        i-1    i
        x            (      ...    )     )

    If x = s[i-dp[i-1]-1] == ), then: dp[i] = dp[i-dp[i-1]-2] + dp[i-1] + 2(because "I did not draw out" in front may still be effective in parentheses string)

    But if the i-dp[i-1]-1symbol of this position is not (it? This shows that with s[i]parenthesis at the end of the pass is not legitimate, namely: d[i] = 0.

    Complete code:

    Require special attention Array Subscript out of range of the problems, once the bounds, the foregoing description is not a valid string bracket.

    /*
        DP解法:
        dp[i] 表示:以s[i]结尾的,最长有效子串
        那么:
            if s[i]=='(' then dp[i] = 0
            if s[i]==')':
                if (s[i-1]=='(') then dp[i] = dp[i-2]+2
                if (s[i-1]==')') then dp[i] = dp[i-dp[i-1]-2] + dp[i-1] + 2
    
     */
    #include "leetcode.h"
    #include <stack>
    class Solution
    {
    public:
        int longestValidParentheses(string s)
        {
            int len = s.length();
            if (len == 0 || len == 1)
                return 0;
            vector<int> dp(len, 0);
            for (int i = 1; i < len; i++)
            {
                if (s[i] == ')')
                {
                    if (s[i - 1] == '(')
                    {
                        if (i >= 2)
                            dp[i] = dp[i - 2] + 2;
                        else
                            dp[i] = 2;
                    }
                    else if (s[i - 1] == ')')
                    {
                        int midlen = dp[i - 1];
                        if (i >= (midlen + 1))
                        {
                            char c = s[i - midlen - 1];
                            if (c == '(')
                                dp[i] = dp[i - 1] + 2 + (i >= (midlen + 2) ? dp[i - midlen - 2] : 0);
                            else
                                dp[i] = 0;
                        }
                        else
                        {
                            dp[i] = 0;
                        }
    
                    }
                }
            }
            for (int x : dp)
                cout << x << ' ';
            cout << endl;
            int result = -1;
            for (int x : dp)
                result = max(result, x);
            return result;
        }
    };
    
    int main()
    {
        string s[] = {"())", "(()", ")()())"};
        Solution sol;
        for (int i = 0; i < 3; i++)
            cout << sol.longestValidParentheses(s[i]) << "\n"
                 << endl;
    }

Guess you like

Origin www.cnblogs.com/sinkinben/p/11516742.html