传送门: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. SpringBoot---Web开发---SSL配置

    1.[生成证书] 2.[SpringBoot配置SSL] 3.[http转向https]

  2. Java编码优化

    Java编码优化 1.尽可能使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中速度较快,其他变 量,如静态变量.实例变量等,都在堆中创建,速度较慢.另外,栈中创建的变量,随 着方 ...

  3. Error: Can't set headers after they are sent.

    Error: Can't set headers after they are sent. 错误:无法设置头信息后发送. 具体报错: 看到了一下代码,自己写错了 没有进行错误判断,两个条件都直接返回, ...

  4. Css+Html

    CSS样式 <style type="text/css"> tt.tt1 { <style type="text/css"> p { b ...

  5. MySQL 查看表大小

    当遇到数据库占用空间很大的情况下,可以用以下语句查找大数据量的表 SELECT TABLE_NAME ,),) 'DATA_SIZE(M)' ,),) 'INDEX_SIZE(M)' ,AVG_ROW ...

  6. c# ExpandoObject动态扩展对象

    js中的Object 对象. php中的stdClass. c# 也有动态可扩展对象 ExpandoObject,需要添加System.Dynamic引用 用法: dynamic model = ne ...

  7. 【Unity3D】魔鬼与牧师游戏记录——MVC架构

    Priests and Devils是一款益智类的小游戏,需要在规定的时间内帮助牧师和魔鬼都安全过河.河边有三个魔鬼和三个牧师,他们都想过河,但河上只有一条船,这艘船每次只能搭载两个,而且必须有一个人 ...

  8. SQL Server Sleeping会话占用内存资源浅析?

      在SQL Server中,会话的状态有运行(Running).睡眠(Sleeping).休眠(Dormant).Preconnect 等状态,有时候你会在数据库中看到很多会话处于睡眠(Sleepi ...

  9. Selenium3+webdriver学习笔记3(xpath方式元素定位)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...

  10. Linux Shell流程例子

    #!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...