POJ:2955-Brackets(经典:括号匹配)
传送门:http://poj.org/problem?id=2955
Brackets
Time Limit: 1000MS Memory Limit: 65536K
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执行的时候,里面先是一段区间一段区间的跑,然后枚举这个区间里面每一种匹配情况,由于在选择区间的时候是从小开始选择,所以在枚举每一种匹配情况的时候都是从前面得到的答案来转移的,这也体现了动态规划,从子问题转移。只不过区间dp里面是从一个小的区间慢慢转移到整个区间。但是怎么处理区间里面的状态要看当前问题的特点。
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn = 110;
char s[maxn];
int dp[maxn][maxn];
bool checke(int S,int E)
{
if(s[S] == '(' && s[E] == ')')
return true;
if(s[S] == '[' && s[E] == ']')
return true;
return false;
}
int main()
{
while(scanf("%s",s))
{
if(strcmp(s,"end") == 0)
break;
memset(dp,0,sizeof(dp));
int len = strlen(s);
for(int i=1;i<len;i++)
for(int j=0,k=i;k<len;k++,j++)
{
if(checke(j,k))
dp[j][k] = dp[j+1][k-1] + 2;
for(int z=j;z<k;z++)
if(dp[j][z] + dp[z+1][k] > dp[j][k])
dp[j][k] = dp[j][z] + dp[z+1][k];
}
printf("%d\n",dp[0][len-1]);
}
return 0;
}
POJ:2955-Brackets(经典:括号匹配)的更多相关文章
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- poj 2955 Brackets
题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...
- CSUOJ 1271 Brackets Sequence 括号匹配
Description ]. Output For each test case, print how many places there are, into which you insert a ' ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
- POJ 2955 Brackets(区间DP)题解
题意:问最多有几个括号匹配 思路:用dp[i][j]表示i到j最多匹配,若i和j构成匹配,那么dp[i][j] = dp[i + 1][j - 1] + 2,剩下情况dp[i][j] = max(dp ...
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
随机推荐
- Ubuntu14.04创建WiFi热点
右键右上角网络图标→Edit Connections →Wi-Fi→Add→Wi-Fi→Create→SSID(wifi名称)→Mode(Ad-hoc)→IPv4 Settings→Methods(S ...
- html select change事件触发
做小组内使用的一个简单工具,其中要实现的一个小功能是当某个下拉菜单的选择值改变时触发另一表单元素的属性变化.自然的想到使用select表单元素的onchange事件. 下拉菜单部分的代码如下: < ...
- Java选择排序算法
package com.jckb; /**选择排序 * * @author gx *算法原理: *第一个数和后面每个数进行比较,如果大于后面的数就进行位置交换, *第一次比较结束后得到了最小值 */ ...
- plsql连接远程数据库快捷方式
不用修改任何文件就可以直接连接远程数据库
- 巧用伪元素绘制带边的三角形--CSS3
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- window.open()弹出窗口参数说明及居中设置
window.open()可以弹出一个新的窗口,并且通过参数控制窗口的各项属性. 最基本的弹出窗口代码 window.open('httP://codeo.cn/'); window.open()各参 ...
- GoDaddy网站程序根目录 网站文件上传到虚拟主机哪个目录
用的linux虚拟主机,网站根目录为public_html,(window主机的目录为httpdocs)我们需要把本地做好的网站上传到此目录下 cPanel控制面板 - 文件管理器 - public_ ...
- Ubuntu 设置静态ip地址
1. 找到文件并作如下修改: sudo vim /etc/network/interfaces 修改如下部分: auto eth0iface eth0 inet staticaddress 192.1 ...
- Cocos2d-x v3.1 初识(一)
Cocos2d-x v3.1 初识(一) Cocos2d-x从以前苹果平台上的Cocos2d发展而来,版本已经更新到了3.1.1.作为一个跨平台的游戏开发引擎,现在已经被上百个国家在使用,这也是国人的 ...
- [Java]获取byte数组的实际使用长度
背景:byte.length只能获取到初始化的byte数组长度,而不是实际使用的长度,因此想要获取到实际的使用长度只能靠其他方法实现. 方法一: public class ByteActualLeng ...