动态规划——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 ...
随机推荐
- PEP8中文翻译(转)
原文:https://github.com/zgia/manual PEP 8 -- Style Guide for Python Code PEP Index > PEP 8 -- Style ...
- js jquery 判断元素是否在数组内(转)
一,js方法 var arr = ["a", "b", "c"]; // js arr.indexOf("c") var ...
- Linux设备树(五 根节点)
五 根节点 一个最简单的设备树必须包含根节点,cpus节点,memory节点.根节点的名字及全路径都是“/”,至少需要包含model和compatible两个属性.model属性我们在属性那节已经说过 ...
- function Language
什么是函数式语言: 函数式语言(functional language)一类程序设计语言.是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据 ...
- 数据库学习之MySQL基础
数据库基础 一.数据库简介 数据库:存放数据的仓库 sql及其规范 sql是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能 ...
- DirectX11 With Windows SDK--09 纹理映射与采样器状态
前言 在之前的DirectX SDK中,纹理的读取使用的是D3DX11CreateShaderResourceViewFromFile函数,现在在Windows SDK中已经没有这些函数,我们需要找到 ...
- oldboy s21day13装饰器和推导式
#!/usr/bin/env python# -*- coding:utf-8 -*- # 2.请为 func 函数编写一个装饰器,添加上装饰器后可以实现:执行func时,先输入"befor ...
- [再寄小读者之数学篇](2014-06-23 Gronwall-type inequality)
Suppose that $$\bex \cfrac{\rd f}{\rd t}+h\leq gf\quad (f,g,h\geq 0,\ t\in [0,T]). \eex$$ Then for $ ...
- EffectiveC++ 第2章 构造/析构/赋值运算
我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 2 构造 / 析构 / 赋值 条款 05:了解C++ ...
- EffectiveC++ 第7章 模板与泛型编程
我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 7 模版与泛型编程 Templates and Gen ...