动态规划——Longest Valid Parentheses
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的更多相关文章
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
- Longest Valid Parentheses——仍然需要认真看看(动态规划)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode32 Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
随机推荐
- java java.net.URLConnection 实现http get,post
抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接.此类的实例可用于读取和写入此 URL 引用的资源.通常,创建一个到 URL 的连接需要几个步骤: open ...
- SNMP学习——v3 VACM
目录: ☆ SNMPv3视图访问控制模型 ☆ SNMPv3报文格式 ☆ VACM参数 ☆ Context Table ☆ Security To Group Table ...
- Nginx--服务部署、基于域名的虚拟主机配置
一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...
- 第三章 Java的基础程序设计结构
一个简单的 Java 应用程序 访问修饰符 public,private,protected main 方法必须时public修饰的,C#则不必须 数据类型 可以用16进制表示浮点数 可以用2,8,1 ...
- kettle 数据迁移
最近在公司搞一个项目重构迁移问题,旧项目一直在线上跑,重构的项目则还没上线.重构之后数据库表结构,字段,类型等都有变化,而且重构的数据库由oracl改为mysql.这样就设计到数据迁移问题,别人推荐下 ...
- 基于 Webhooks gitlab 自动化构建
基于gitlab webhooks 自动构建流程 1.服务器安装 git 服务 安装成功 配置 PHP 脚本: <?php // 接受头部信息 if (!isset($_GET['youpara ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- vs 快捷操作
1. 选中所需行 增加缩进 tab 减少缩进 shift+tab 2.附加调试:ctrl+alt+p: 全部用快捷键操作看起来真的很6
- sed 使用行号与关键字匹配限定行范围
1.打印匹配数字4 到最后一行 [111 sed]$ cat input [111 sed]$ sed -n '/4/,$p' input
- selenium——find_element_by_xx 与 find_element(By.XX,'XXXX')