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.

这道题好理解,但是要自己想的话还是很难想出来的,动态规划说白了就是记录当前的结果,留给后面的用。

求最长合法匹配的长度,这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1] 包含s[i] 的最长的有效匹配括号子串长度。则存在如下关系:

  • dp[s.length - 1] = 0;
  • i从n - 2 -> 0逆向求dp[],并记录其最大值。若s[i] == '(',则在s中从i开始到s.length - 1计算dp[i]的值。这个计算分为两步,通过dp[i + 1]进行的(注意dp[i + 1]已经在上一步求解):
    • 在s中寻找从i + 1开始的有效括号匹配子串长度,即dp[i + 1],跳过这段有效的括号子串,查看下一个字符,其下标为j = i + 1 + dp[i + 1]。若j没有越界,并且s[j] == ‘)’,则s[i ... j]为有效括号匹配,dp[i] =dp[i + 1] + 2。
    • 在求得了s[i ... j]的有效匹配长度之后,若j + 1没有越界,则dp[i]的值还要加上从j + 1开始的最长有效匹配,即dp[j + 1]。
      class Solution {
      public:
      int longestValidParentheses(string s) {
      int len=s.size();
      vector<int >res(len,);
      int finalRes=;
      for(int i = len-;i>=;i--)
      {
      if(s[i]=='(')
      {
      int end=i + + res[i + ];
      if(end<len&&s[end]==')')
      {
      res[i]=res[i+]+;
      if(end+<len)
      res[i]+=res[end+];
      finalRes=max(finalRes,res[i]);
      } }
      }
      return finalRes;
      }
      };

      最简单的方法:当碰到")"时,向左遍历找到未标记过的"(",则将这两个位置标为1,然后统计1连续出现的长度即可,时间复杂度为O(n^2)。

      http://blog.csdn.net/cfc1243570631/article/details/9304525

Longest Valid Parentheses——仍然需要认真看看(动态规划)的更多相关文章

  1. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

  2. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 动态规划——Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  7. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  8. Longest Valid Parentheses(最长有效括号)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. LeetCode32 Longest Valid Parentheses

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

随机推荐

  1. 51nod 1275 连续子段的差异(twopointer+单调队列)

    对于每一个i找到最近的j满足最大值-最小值>K,对答案的贡献为j-i,用单调队列维护最值即可 #include<iostream> #include<cstdlib> # ...

  2. matlab特殊符号表

    Character Sequence Symbol Character Sequence Symbol Character Sequence Symbol \alpha α \upsilon υ \s ...

  3. noip模拟赛 但有用

    题目描述 给定一个 n ∗ m 个矩阵,矩阵中每个数都是 [1, 12] 内的整数.你可以执行下列两个操作任意多次: • 指定一行,将该行所有数字 +1. • 指定一列,将该列所有数字 +1. 如果执 ...

  4. [技巧篇]16.MyEclipse2014安装SVN插件,在线安装

    这两天想做点东西,但是现在流行的是git,但是军哥的项目是托管到阿里的svn代码当中,所以没有办法,还需要弄SVN,这里不将什么安装SVN等东西 我要求的就是快速入门而已,仅此而已,我安装成功,过程很 ...

  5. MongoDB入门(5)- 我们自己封装的MongoDB-Java版本

    用法 实体定义 package com.wisdombud.mongotool; import java.io.Serializable; import java.util.Date; import ...

  6. 用reduce实现简单的pipe

    function pipe(src, ...fns){ return fns.reduce(function(fn1, fn2){ return fn2(fn1) }, src); } undefin ...

  7. 【BZOJ1221】【HNOI2001】软件开发 [费用流]

    软件开发 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 某软件公司正在规划一项n天的软件开 ...

  8. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  9. adb操作指令大全

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试android程序,说白了就是debug工具.a ...

  10. 火狐浏览器下点击a标签时出现虚线的解决方案

    1.兼容性问题 火狐浏览器下点击a标签时出现虚线 2.解决方案 a:focus { outline: none;}