poj 2955 Brackets (区间dp 括号匹配)
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
题意:现在已知一个由'()''[]'组成的括号序列 问其中括号匹配数最大的子序列个数
思路:
这里的状态转移是以一个if为基础的,如果s[i]与s[j]匹配,那么明显的dp[i][j] = dp[i+1][j-1];然后在这个基础上枚举分割点k.
状态转移方程:dp[i][j]表示第i~j个字符间的最大匹配对数。
if(s[i] 与 s[j]匹配) dp[i][j] = d[[i+1][j-1] +1;
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
最后乘2即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int dp[][];
string s;
int jug(int i,int j){
return (s[i]=='['&&s[j]==']')||(s[i]=='('&&s[j]==')');
}
int main(){
ios::sync_with_stdio(false); while(cin>>s){
if(s=="end") break;
memset(dp,,sizeof(dp));
int n=s.length();
for(int len=;len<=n;len++){
for(int i=;i+len<=n+;i++){
int j=i+len-;
dp[i][j]=dp[i+][j-]+jug(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 ;
}
poj 2955 Brackets (区间dp 括号匹配)的更多相关文章
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- 区间dp 括号匹配问题
这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...
- 区间dp - 括号匹配并输出方案
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
随机推荐
- Python第八天 模块 包 全局变量和内置变量__name__ Python path
Python第八天 模块 包 全局变量和内置变量__name__ Python path 目录 Pycharm使用技巧(转载) Python第一天 安装 shell 文件 Pyt ...
- Windows server 2008R2远程桌面3389端口修改方法技巧
windows server的服务器远程桌面默认端口号是3389,在工作中经常使用远程桌面连接服务器,但是这也是常常被黑客利用的端口号,但是如何修改掉默认端口,预防被黑客利用呢? 可以如下操作配置:很 ...
- 有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value替换为A中对应的value
update B b set b.value=(select max(a.value) from A a where b.key=a.key) from A c where b.key=c.key) ...
- VMware虚拟机上网络连接解决方案
VMware虚拟机上网络连接解决方案 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 从虚拟机上连接外部网络,需要设置以下几个地方. 1.服务 (1)打开 ...
- python网络进阶篇
并发编程 进程 操作系统的历史 # 手工操作 —— 穿孔卡片 # 程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入输入机,然后启动输入机把程序和数据输入计算机内存,接着通过控制台开关启动程序针对数 ...
- Starting sshd: /var/empty/sshd must be owned by root and not group or world-writable.
Starting sshd: /var/empty/sshd must be owned by root and not group or world-writable. [FAILED] ...
- iowait 过高问题的查找及解决linux
Linux 有许多可用来查找问题的简单工具,也有许多是更高级的 I/O Wait 就是一个需要使用高级的工具来debug的问题,当然也有许多基本工具的高级用法.I/O wait的问题难以定位的原因是因 ...
- 好程序员web前端开发测验之css部分
好程序员web前端开发测验之css部分Front End Web Development Quiz CSS 部分问题与解答 Q: CSS 属性是否区分大小写? <p><font si ...
- Openwrt无线中继设置并访问外网
Openwrt无线中继设置并访问外网 本篇博文参考来自:http://blog.csdn.net/pifangsione/article/details/13162023 配置目标 主路由器使用AP模 ...
- 【转】localStorage使用总结
原文地址:https://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...