POJ2955(KB22-C 区间DP)
Brackets
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
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)的更多相关文章
- poj2955括号匹配 区间DP
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5424 Accepted: 2909 Descript ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ2955 Brackets (区间DP)
很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...
- 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 ...
- HDU4632 Poj2955 括号匹配 整数划分 P1880 [NOI1995]石子合并 区间DP总结
题意:给定一个字符串 输出回文子序列的个数 一个字符也算一个回文 很明显的区间dp 就是要往区间小的压缩! #include<bits/stdc++.h> using namesp ...
- POJ2955 Brackets —— 区间DP
题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- 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 ...
- poj2955:括号匹配,区间dp
题目大意: 给一个由,(,),[,]组成的字符串,其中(),[]可以匹配,求最大匹配数 题解:区间dp: dp[i][j]表示区间 [i,j]中的最大匹配数 初始状态 dp[i][i+1]=(i,i+ ...
- POJ2955【区间DP】
题目链接[http://poj.org/problem?id=2955] 题意:[].()的匹配问题,问一个[]()串中匹配的字符数,匹配方式为[X],(X),X为一个串,问一个长度为N(N<= ...
随机推荐
- java打包jar后,使之一直在linux上运行,不随终端退出而关闭
nohup java -jar xxx.jar&
- 【dpdk】使用libpcap-PMD驱动收发包
ref: Dpdk programmer’s guide 1. 概述 dpdk不仅提供针对物理和虚拟网卡的pmd驱动(Poll Mode Drivers),还提供两个纯软件的pmd驱动,libpca ...
- java后端导入excel将数据写入数据库
参考:https://www.cnblogs.com/hanfeihanfei/p/7079210.html @RequestMapping("/importExcel.do") ...
- django~项目的文件位置的重要性
前几天我犯了个很低级的错误 就是把文件的地址放错地方了~~ 我把templates文件放进mysite文件里面了 和templatetags文件同级了 所以一直报错 说找不到模板的文件 实际上te ...
- 详解使用flask_paginate进行分页
分页技术好处: 1.分页技术是把数据全部查询出来,然后再进行分页 2.分页技术可以,降低带宽使用,提高访问速度 使用flask_paginate进行分页 1.要使用flask_paginate,首先安 ...
- (转)csv — 逗号分隔值文件格式
原文:https://pythoncaff.com/docs/pymotw/csv-comma-separated-value-files/125 csv 模块主要用于处理从电子数据表格或数据库中导入 ...
- Java8-函数复合用法
JDK8自带的函数式接口Function有两个默认方法andThen和compose,它们都返回Function的一个实例,可以用这两个方法把Function接口所代表的的Lambda表达式复合起来. ...
- SpringSecurity学习之快速上手
互联网项目中,安全与权限控制是不可回避的问题,为了解决这一些列问题,许多安全框架应运而生了.这些框架旨在帮我们解决公用的安全问题,让我们的程序更加健壮,从而让程序员全身心投入到业务开发当中.那么Spr ...
- Linux ulimit和动态修改MySQL最大线程数限制
ulimit是限制进程对资源的使用但软件资源限制变化不大,特别是process/file,分别对应nproc和nofilenproc可用 ulimit -u 查询:nofile可用 ulimit -n ...
- Unity3D第一战:软件安装与代码调试
1.软件的安装 Unity3D可以轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,功能非常强大. 最新版下载地址:http://unity3d.com/u ...