HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets
My Tags (Edit)
Source : Stanford ACM Programming Contest 2004
Time limit : 1 sec Memory limit : 32 M
Submitted : 188, Accepted : 113
5.1 Description
We give the following inductive definition of a “regular brackets” sequence:
• the empty sequence is a regular brackets sequence,
• if s is a regularbrackets 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,…,im where 1 ≤ i1 < i2 < …< im ≤ n, ai1ai2 …aim is a regular brackets sequence.
5.2 Example
Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].
5.3 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. For example:
((()))
()()()
([]])
)[)(
([][][)
end
5.4 Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line. For example:
6
6
4
0
6
一道简单的区间DP题目
关于区间DP,可以参照这个博客
http://blog.csdn.net/dacc123/article/details/50885903
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
char a[105];
int dp[105][105];
int main()
{
while(scanf("%s",a+1)!=EOF)
{
if(a[1]=='e')
break;
memset(dp,0,sizeof(dp));
int n=strlen(a+1);
for(int len=1;len<n;len++)
{
for(int i=1;i+len<=n;i++)
{
int j=i+len;
if((a[i]=='('&&a[j]==')')||(a[i]=='['&&a[j]==']'))
dp[i][j]=dp[i+1][j-1]+2;
else
dp[i][j]=dp[i+1][j-1];
for(int k=i;k<j;k++)
{
if(dp[i][j]<dp[i][k]+dp[k+1][j])
dp[i][j]=dp[i][k]+dp[k+1][j];
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}
HOJ 1936&POJ 2955 Brackets(区间DP)的更多相关文章
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- A - Brackets POJ - 2955 (区间DP模板题)
题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...
- POJ 2955 Brackets 区间合并
输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp ...
随机推荐
- C++ 类的头文件、实现、使用
再次吐槽下C++Primer这本书,啰哩啰嗦,废话太多.如果我来翻译的话,绝对删减一堆没用的---仅限于发牢骚. 不知道是否经典的做法 类中的成员声明在头文件中,定义(我更喜欢叫实现)在源文件中,使用 ...
- 标准的 C++ 由三个重要部分组成
标准的 C++ 由三个重要部分组成: 核心语言,提供了所有构件块,包括变量.数据类型和常量,等等.C++ 标准库,提供了大量的函数,用于操作文件.字符串等.标准模板库(STL),提供了大量的方法,用于 ...
- C# 发邮件 服务器响应为: 5.7.0 Must issue a STARTTLS command first
The SMTP server requires a secure connection or the client was not authenticated. The server respons ...
- bowtie2-inspect 根据bowtie2的索引取得fasta 序列
今天运行tophat2的时候看到下面这条记录: [2016-02-27 11:40:03] Checking for reference FASTA file Warning: Could not f ...
- [渗透技巧] Windows命令行下载
certutil简介 用于证书管理 支持环境: XP - Windows 10 全系统 更多:https://technet.microsoft.com/zh-cn/library/cc75534 ...
- 算法--将Excel列索引转换成默认标识
使用POI导入Excel时,有时对模板进行验证,假如第1行第1列错误,此时的rowIndex=columnIndex=0,与平时看到的Excel行列标记相比不太直观,因此通过相应的算法将其转换成人们熟 ...
- MongoDB基础入门视频教程
MongoDB基础入门视频教程http://www.icoolxue.com/album/show/98
- 怎么用MathType编辑带圈字母
在用word公式编辑器MathType编辑公式时,里面涉及到很多的数学物理符号等等,这些符号或者是公式其实都可以利用MathType中的模板进行组合.在这些数学公式中,有时会有一些比较特殊的符号,它们 ...
- Django管理工具django-admin.py创建项目
安装Django之后就安装了可用的管理工具django-admin.py,可以使用它来创建项目,运行django-admin.py来查看命令介绍:
- MySQL性能优化(十)-- 主从复制(一主多从)
环境准备: Linux1(matser) Linux2(slave) Linux3(slave) 搭建 1.先清空原来的master和slave配置 reset master; 2.