Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()" 大概意思就是,提供一个只含'('和')'的字符串S,请在S中找到一个'('和')'配对出现的最长连续子字符串,输出其长度这个最长连续子字符串可以是“(())”这种,也可以是“()()”这种,而且必须是连续的。
这个题的状态相对来说比较好想,状态dp[i]:包含第i个字符的最长连续子串的长度,前提是包含当前这个字符S[i](因为要保证结果是个连续的字串),所以可以想像到所有'('处的dp值都是0。这样问题就来了
状态转移方程是什么呢?考虑到'('和')'要配对,不妨两个两个的看S的字符。仔细研究所有字符串的情况可以发现,所有符合要求的无非就两种情况:(1)...().... (2)...((...))...两个两个的看S的字符,
(1)(2)中没有包含的连续的两个字符的情况不用考虑,像什么S[i]=='('和S[i-1]==')'之类的,都不用管,没用!
对于情况一,即S[i]==')'和S[i-1]=='('这种直接配对的情况,直接dp[i] = dp[i-2]+2,道理不赘述,注意数组访问不要越界
对于情况二,稍稍复杂一些,这种情况就是大的套小的,我们要查看小套的上界,也就是大套的左边是不是'(',如何访问这个大套的左侧字符呢,认真思考后可以想到dp[i-1]代表包含了S[i-1]的最长连续子串的长度,S[i]
是大套的右边界')',S[i-1]是小套的右边界')',则S[i-dp[i-1]]是小套的左边界,(当然这个dp[i-1]等于0也就不用管了没意义的),易知S[i-dp[i-1]-1]是大套的左边界,如果S[i-dp[i-1]-1]='('说明能配对,同时要注意
到dp[i-dp[i-1]-1]可能不等于0,也就是说大套的左侧可能还有符合要求的字串,如果存在的话应该连起来,所以这种情况下dp[i] = dp[i-1]+dp[i-dp[i-1]-2]+2,即小套长度+大套左侧的长度+套左右边界两个字符的长度2;
如果S[i-dp[i-1]-1]=')'说明不能配对,此时不管用了,让dp[i]保持默认值0即可。当然这种情况也要注意数组访问不能越界! 此题还有设置一个变量ans来记录最长的这个长度,因为S中可能断断续续有好几个离散的符合要求的'('和')'配对的字串,需要这个变量ans来不断筛选最优解,操作很简单,每次求出一个dp[i],就更新一次ans的值,
即:ans = ans > dp[i]?ans:dp[i] 代码如下:
 int longestValidParentheses(string s) {
int ans = ;
int slen = s.length();
if (slen <= )return ans;
int* dp = new int[slen];
for (int i = ; i < slen; i++)
dp[i] = ;
if (s[] == '('&&s[] == ')')dp[] = ;
ans = dp[];
for (int i = ; i < slen; i++){
if (s[i] == ')'){
if (s[i - ] == '(')dp[i] = dp[i - ] + ;
else{
if ((i - dp[i - ] - ) == && s[i - dp[i - ] - ] == '(')dp[i] = dp[i - ] + ;
else if ((i - dp[i - ] - ) > && s[i - dp[i - ] - ] == '(')dp[i] = dp[i - ] + dp[i - dp[i - ] - ] + ;
}
}
ans = ans > dp[i] ? ans : dp[i];
}
delete[]dp;
return ans;
}
												

动态规划——Longest Valid Parentheses的更多相关文章

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

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

  2. Longest Valid Parentheses——仍然需要认真看看(动态规划)

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

  3. [LeetCode] 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. npm后台启动nuxt服务之 kill

    后台启动 npm run start & ps aux | grep start 根据项目对应的id执行如下命令 kill xxxx

  2. 随手记一个漂亮的code

    代码  从前有个代码长这样 if (a) { if (b) { c } } else { if (d) { c } } 后来长这样 if (a && b || !a && ...

  3. 金融量化分析【day112】:股票数据分析Tushare2

    目录 1.使用tushare包获取某股票的历史行情数据 2.使用pandas包计算该股票历史数据的5日局限和60日均线 3.matplotlib包可视化历史数据的收盘价和历史均线 4.分析输出所有金叉 ...

  4. 腾讯云服务器tomcat端口无法访问

    第一种情况: 如题:https://console.cloud.tencent.com/cvm/securitygroup 需要去这个地址设置安全组. 说实话,一句mmp不知当讲不当讲.使用说明这块太 ...

  5. Form -------- 使用

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 1.创建Form类 from django.f ...

  6. npm常用命令学习(npm install -D,semver版本规范, npm进行版本管理的最佳实践用法)

    什么是npm npm有两层含义.一层含义是Node的开放式模块登记和管理系统,网址为npmjs.org.另一层含义是Node默认的模块管理器,是一个命令行下的软件,用来安装和管理Node模块. npm ...

  7. 深入浅出mybatis之映射器

    目录 概述 XML映射器 定义xml映射器 配置xml映射器 使用xml映射器 接口映射器 定义接口映射器 配置接口映射器 使用接口映射器 总结与对比 概述 映射器是MyBatis中最核心的组件之一, ...

  8. SpringBoot系列: Spring项目异常处理最佳实践

    ===================================自定义异常类===================================稍具规模的项目, 一般都要自定义一组异常类, 这 ...

  9. [再寄小读者之数学篇](2014-06-23 Hardy 空间、BMO空间与 Triebel-Lizorkin 空间)

    $$\bex 0<p<\infty\ra H_p=\dot F^0_{p,2};\quad BMO=\dot F^0_{\infty,2}. \eex$$ see [H. Triebel, ...

  10. uploadPreview上传图片前预览图片

    uploadPreview.js是一款图片上传前的预览插件.谷歌.火狐.IE都可以兼容,但是不支持safari. 相关的html代码: <!DOCTYPE html PUBLIC "- ...