POJ 2955 Brackets (区间dp入门)
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, …, im where 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 题意给你一个只含()[]的字符串,问你最多能配成对的有多少个和字符。
区间dp的入门题。整理下思路dp[i][j]表示区间i~j之间最大的匹配字符数。
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')) ————>dp[i][j]=dp[i+1][j-1]+2; 懂吧
代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream> using namespace std;
char s[];
int dp[][];
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%s",&s))
{
if (s[]=='e')
break ;
memset(dp,,sizeof dp);
int len=strlen(s);
for (int k=;k<len;++k)
{
for (int i=,j=k;j<len;++i,++j)
{
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[i][j]=dp[i+][j-]+;
for (int x=i;x<j;x++)
dp[i][j]=max(dp[i][j],dp[i][x]+dp[x+][j]);
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
POJ 2955 Brackets (区间dp入门)的更多相关文章
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- 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)
题目链接 #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 ...
随机推荐
- Shiro安全框架的说明及配置入门
Shiro是什么? Shiro是一个非常强大的,易于使用的,开源的,权限框架.它包括了权限校验,权限授予,会话管理,安全加密等组件 什么时候使用它呢? 如果你是设计RBAC基础系统,需要编写大量用于权 ...
- php trim()函数 语法
php trim()函数 语法 trim()函数怎么用? php trim()函数用来删除字符串两端的空格或其他预定义字符,语法是trim(string,charlist),返回经过charlist处 ...
- shell(计算机壳层)(二)
shell 命令常用命令cat 文件名 输出文件内容到基本输出(屏幕 or 加>fileName 到另一个文件)cb 格式化源代码chmod //change mode,改变文件的权限cp co ...
- Agri-Net —poj1258
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44670 Accepted: 18268 Descri ...
- [CSP-S模拟测试]:Race(数学+Trie树)
题目描述 一年一度的运动会开始了.有$N$个选手参赛,第$i$个选手有一个能力值(保证$A[i]$两两不同),比赛一共进行了天.在第$j$天($0\leqslant j\leqslant 2^{m-1 ...
- maven scope 作用域
1.test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖 2.compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去 3.provided依赖:在编译和测试的过程有效,最后 ...
- 一些关于SEO优化的笔记
高级搜索指令: 双引号:“xxx” 代表完全匹配的搜索 减号:-(减号前面必须是空格,后面必须没有空格)代表搜索不包含减号后面的词的页面 filetype:用于搜索特定文件格式(百度支持的文件类型:P ...
- Loadrunner实现多台测试机负载时可能遇到的难题
压测机安装的loadrunner版本为11.0 压测机器的操作系统是windows server 2016 所有的负载机器操作系统和压侧机器是一样的,都是windows server 2016 Err ...
- IPython notebook在浏览器中显示不正常的问题及解决方法
使用过Python的童鞋们应该知道IPython是一个比python自带的交互式界面更加友好的交互界面,IPython提供了自动补齐什么的,其实我还没开始用所以这里也不扯淡了,大家自己去网上查,IPy ...
- iOS 自己写的对话框中加入三个输入框
-(void)dialog:(NSString*)title okTitle:(NSString*)okTitle placeholder:(NSString*)placeholder finish: ...