Brackets(区间dp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3624 | Accepted: 1879 |
Description
We give the following inductive definition of a “regular brackets” sequence:
- the empty sequence is a regular brackets sequence,
- if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
- if a and b are regular brackets sequences, then ab is a regular brackets sequence.
- no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, imwhere 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6
Source
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char st[] ;
int a[][] ; bool check (int a , int b)
{
if (st[a] == '(' && st[b] == ')' )
return ;
if (st[a] == '[' && st[b] == ']' )
return ;
return ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while () {
gets (st) ;
if (strcmp (st , "end") == )
break ;
memset (a , , sizeof(a) ) ;
int len = strlen (st) ;
for (int o = ; o <= len ; o++) {
for (int i = ; i < len - o + ; i++) {
int j = i + o ;
for (int k = i ; k < j ; k++ ) {
a[i][j - ] = max (a[i][j - ] , a[i][k] + a[k + ][j - ] ) ;
if (check (i , j - ) ) {
a[i][j - ] = max (a[i][j - ] , a[i + ][j - - ] + ) ;
}
}
}
}
printf ("%d\n" , a[][len - ] ) ;
}
return ;
}
区间dp感觉和merge sort有异曲同工之妙
从可行的最小区间出发,逐级上去,最终得到整段区间的最终结。
dp[i][j] 指[i , j]这段区间的最优解
Brackets(区间dp)的更多相关文章
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ2955 Brackets —— 区间DP
题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- JQuery上传文件插件Uploadify使用笔记
新工作的第一份任务就是给实现 限制Uploadify 上传文件格式为图片 测试出来报错,选择了非图片文件,提示错误后,再选择其他文件,上传时还是包含了之前清空的非图片文件 最后实现效果的代码是 //上 ...
- 我的第二个app上线:术购管家
忙了两周写完的app,终于发布了,可是等上线竟然等了两周多,今天终于上线了,一路顺畅,没有被打回过...
- 使用jquery获取url及url参数的方法及定义JQuery扩展方法
1.jquery获取url很简单,代码如下: window.location.href; 其实只是用到了javascript的基础的window对象,并没有用jquery的知识. 2.jquery获取 ...
- [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2186 分析: 就是要求1~n!中与m!互质的数的个数 首先m!以内的就是φ(m!) 关 ...
- DOM(六)事件类型
对于用户事件类型而言,最常用的是鼠标.键盘.浏览器. 1.鼠标事件: 鼠标的事件都频繁使用,下面例子就测试各种鼠标事件 <script language="javascript&quo ...
- .Net MVC中访问PC网页时,自动切换到移动端对应页面
随着移动端的流行,越来越的网站,除了提供PC网页之外,也提供了移动端的H5页面,手机在访问www.xxx.com的时候,能自动跳转到mobile.xxx.com.网上很多在实现时也能使用JS直接进行跳 ...
- PowerDesigner-导出表到word
1. 在工具栏中选择[Report -->Reports],如下图 2. 点击第二个图标创建一个Report,如下图 该wizard中有三个信息 Report name Report : Rep ...
- java.io.FileNotFoundException:文件名、目录名或卷标语法不正确
出现次错误的原因主要是在windows系统下创建文件需要遵循其文件名的规则.导致创建文件失败,从而提示filenotfound异常,文件未找到 Windows 中文件夹命名规则是: ① 文件名或文件夹 ...
- sql-in和not in
IN .NOT IN这个指令可以让我们依照一或数个不连续 (discrete) 的值的限制之内抓出数据库中的值 in和not in in:存在与...里面的 not in:不存在与..里面的 其指令语 ...
- c#截图
private void Form_Load(object sender, EventArgs e){ //接收web url string colle = string.Empty; stri ...