Brackets

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7823Accepted: 4151

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 i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … 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

Source

 
 //2017-05-22
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int dp[][];//dp[l][r]表示区间l-r中括号匹配数
//若位置l和r匹配,dp[l][r] = max(dp[l][r], dp[l+1][r-1]+2)
//否则,dp[l][r] = max(dp[l][r], dp[l][k]+dp[k+1][r] int main()
{
string str;
while(cin>>str)
{
if(str[] == 'e')break;
int n = str.length();
memset(dp, , sizeof(dp));
for(int len = ; len < n; len++){
for(int i = ; i+len < n; i++){
int j = i+len;
if((str[i] == '(' && str[j] == ')') || (str[i] == '[' && str[j] == ']'))dp[i][j] = max(dp[i][j], dp[i+][j-]+);
for(int k = i; k <= j; k++)
dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+][j]);
}
}
cout<<dp[][n-]<<endl;
} return ;
}

POJ2955(KB22-C 区间DP)的更多相关文章

  1. poj2955括号匹配 区间DP

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5424   Accepted: 2909 Descript ...

  2. POJ2955:Brackets(区间DP)

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

  3. POJ2955 Brackets (区间DP)

    很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...

  4. POJ2955 Brackets(区间DP)

    给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+d ...

  5. HDU4632 Poj2955 括号匹配 整数划分 P1880 [NOI1995]石子合并 区间DP总结

    题意:给定一个字符串 输出回文子序列的个数    一个字符也算一个回文 很明显的区间dp  就是要往区间小的压缩! #include<bits/stdc++.h> using namesp ...

  6. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  7. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  8. poj2955 区间dp

    //Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...

  9. poj2955:括号匹配,区间dp

    题目大意: 给一个由,(,),[,]组成的字符串,其中(),[]可以匹配,求最大匹配数 题解:区间dp: dp[i][j]表示区间 [i,j]中的最大匹配数 初始状态 dp[i][i+1]=(i,i+ ...

  10. POJ2955【区间DP】

    题目链接[http://poj.org/problem?id=2955] 题意:[].()的匹配问题,问一个[]()串中匹配的字符数,匹配方式为[X],(X),X为一个串,问一个长度为N(N<= ...

随机推荐

  1. Linux下查看文档指令

    1,cat直接查看 cat /etc/profile 2,more,less分页查看 less  /etc/profile more /etc/profile less -N /etc/profile ...

  2. java批量读取多个文件并存入数据库

    有时候服务运行的日志文件,需要统计分析,但数据量很大,并且直接在文件中看很不直观,这时可以将文件中的内容导入到数据库,入库后的数据就可以按照需求进行统计分析了. 这个是以服务器的访问日志作为示例,一个 ...

  3. Go语言学习笔记(1)——Hello World!

    第一个go程序——HelloWorld.go 源码 : package main import ("fmt") // import "fmt" func mai ...

  4. 13-01 java StringBuffer类,StringBuilder类

    StringBuffer类的构造方法 package cn.itcast_01; /* * 线程安全(多线程讲解) * 安全 -- 同步 -- 数据是安全的 * 不安全 -- 不同步 -- 效率高一些 ...

  5. tensorflow进阶篇-5(反向传播2)

    上面是一个简单的回归算法,下面是一个简单的二分值分类算法.从两个正态分布(N(-1,1)和N(3,1))生成100个数.所有从正态分布N(-1,1)生成的数据目标0:从正态分布N(3,1)生成的数据标 ...

  6. Java 范例 - 线程

    创建线程 Java 中有以下三种方式创建线程,其中前两种无法获取返回值,而最后一种可以获取返回值. 实现 Runnable 接口 继承 Thread 类 通过 Callable.Future 接口配合 ...

  7. tomcat 配置文件server.xml 详解 Connector Engine Host Context

    目录 一 server.xml 1.1 server 配置 1.2 service 配置 1.3 Executor 1.4 Connector 配置 1.5 Engine 其他tocmat 文章 一 ...

  8. 价值 1500 美元的 iPhone 值得买吗

    原文链接:价值 1500 美元的 iPhone 值得买吗 最新款 iPhone 的最高配型号在含税的情况下价格远超 1500 美元.价格合理吗?合理.理由如下:1,硬件已与笔记本电脑相当,价格也相当: ...

  9. 导入maven项目遇到中文乱码

    windows->preferences->content types->word Document 并输入utf-8->update; 右键选中的项目,选择propertie ...

  10. StreamReader 的ReadLine,Read,ReadToEnd方法

    1.ReadLine() 当遇到\n \r 或者是\r\n的时候 此方法返回这前面的字符串,然后内部的指针往后移一位下次从新的地方开始读,直到遇到数据的结尾处返回null,所以经常这样使用       ...