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. php支持大文件上传

    打开php.ini找到 upload_max_filesize . memory_limit . post_max_size 这三个参数! upload_max_filesize = 2G 是上传最大 ...

  2. 基于USB网卡适配器劫持DHCP Server嗅探Windows NTLM Hash密码

    catalogue . DHCP.WPAD工作过程 . python Responder . USB host/client adapter(USB Armory): 包含DHCP Server . ...

  3. 第九节: 利用RemoteScheduler实现Sheduler的远程控制

    一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上控制这个Scheduler. 2. 猜想: A服务器上的Scheduler需要有 ...

  4. Linux运维(首页)

    系统学习,以此见证学习历程 Linux运维基础 安装vmware+centos Linux基础 Linux的一些问题 Ubuntu遇到的bug linux_网易云音乐安装 linux_添加图标 遇到的 ...

  5. css 兼容各种iPhone

    @media (device-height:480px) and (-webkit-min-device-pixel-ratio:2){/* 兼容iphone4/4s */ .class{} } @m ...

  6. 代码,python1

    def main(): try: number1,number2=eval(input("please enter two number")) result=number1/num ...

  7. django登录

    一. form表单使用注意事项: 1. action="" 提交地址, method='post' 请求方式 2. input 标签要有name属性才能被获取 3. 有一个inpu ...

  8. JAVA进阶7

    间歇性混吃等死,持续性踌躇满志系列-------------第7天 1.Map接口的常用方法 import java.util.HashMap; import java.util.Map; publi ...

  9. 【转】Python3 操作符重载方法

    Python3 操作符重载方法 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/70769628 以下代码 ...

  10. Innodb和Myisam数据恢复

    (转自)https://www.cnblogs.com/DwyaneTalk/p/4113829.html 背景 这次恢复oracle和sqlserver,想想也不能把mysql落下了吧.三剑合一.都 ...