传送门: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


  1. 这是一个区间dp的经典例题,刚开始的时候还以为可以模拟做,但是事实就是这是不行的,只是自己想的太简单,括号的匹配有很多种。
  2. 区间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(经典:括号匹配)的更多相关文章

  1. POJ 2955 Brackets --最大括号匹配,区间DP经典题

    题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...

  2. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  3. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

  4. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  6. poj 2955 Brackets

    题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...

  7. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

  8. Poj 2955 brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7795   Accepted: 4136 Descript ...

  9. 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 ...

  10. poj 2955 Brackets dp简单题

    //poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...

随机推荐

  1. 修改Tomcat和Jetty默认JDK

    tomcat: sed -i 's/java-7-oracle/java-8-oracle/g' /etc/init.d/tomcat7 Jetty echo 'JAVA_HOME=/usr/lib/ ...

  2. (转)启动网卡报错(Failed to start LSB: Bring up/down networking )解决办法总结

    启动网卡报错(Failed to start LSB: Bring up/down networking )解决办法总结 原文:http://blog.51cto.com/11863547/19059 ...

  3. 如何使用Spring Security手动验证用户

    1.概述 在这篇快速文章中,我们将重点介绍如何以编程方式在Spring Security和Spring MVC中设置经过身份验证的用户. 2. Spring Security 简而言之,Spring ...

  4. Vue2之页面 、js 、css分离

    在编写vue的时候,页面数据少的时候,可以将所有的js和css都可以直接写在页面上,但是页面数据多,js.css的方法和样式多的时候,都放在一个页面的时候,就显得页面vue十分的臃肿. 所以写项目的时 ...

  5. Java基础:(四)继承

    一.访问权限 java中有三个访问权限修饰符private:protected:public和一个默认default(不加修饰符). 如果子类的方法覆盖了父类的方法,那么子类中该方法的访问级别不允许低 ...

  6. linux 环境下备份oracle 数据库

    登陆linux后,进入oracle的安装目录下,找到bin那个目录,进入bin目录ls -l 看这些命令的所有者: su - oracle这时会进入这个用户的主目录/home/oracle,此时,可以 ...

  7. 使用git和github管理项目代码

    以前不知道使用代码管理工具,最后写的一些东西都没有了,由于硬盘坏了或者不小心格式化了之类的,后来使用了Git 和Github来托管自己的代码和读书笔记方便了不少,到哪里只要有网就可以把自己的东西拷贝下 ...

  8. 纪念Google Reader—Google Reader的最后一天

    从2006年到今天,几乎每天我都会打开Google Reader,但是今天不一样,因为它是最后一天.心情有些依依不舍,像是与一位多年老朋友永别.因此我非常痛恨Google,先给你送来个好朋友,再从你身 ...

  9. python+selenium之多表单切换

    在Web应用中经常会遇到fram/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于fram/iframe表单内嵌套页面上的元素无法直接定位.这是需要通过swit ...

  10. Android(java)学习笔记126:判断SD卡状态和SD卡容量

    1. 判断SD卡状态和SD卡存储空间大小 当我们在使用SD卡时候,如果我们想往SD卡里读写数据,我们必须在这之前进行一个逻辑判断,那就是判断SD卡状态和SD存储空间大小: 核心代码: String s ...