poj2955括号匹配 区间DP
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5424 | Accepted: 2909 |
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
4
0
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
char s[MAXN];
int ok(char k1,char k2)
{
if(k1 == '(' && k2 == ')')
return ;
if(k1 == '[' && k2 == ']')
return ;
return ;
}
int main()
{
while(~scanf("%s",s+)){
if(s[] == 'e')break;
int len = strlen(s+);
int ans = ;
memset(dp,,sizeof(dp));
for(int i = ; i <= len; i++){
for(int j = ; j <= len - i + ; j++){
if(i == && ok(s[j],s[j+i-])){
dp[j][i] = ;
}
else if(i > ){
int p = ;
for(int k = ; k <= i - ; k++){
p = max(p,dp[j+][k]+dp[j+k+][i-k-]);
//cout<<i<<endl;
//cout<<dp[j+1][k]<<' '<<j+1<<' '<<k<<' '<<endl;
//cout<<dp[j+k+1][i-k-1]<<' '<<j+k+1<<' '<<i-k-1<<' '<<endl;
}
if(ok(s[j],s[j+i-])){
p++;
}
dp[j][i] = max(p,dp[j][i]);
for(int k = ; k <= i; k++){
dp[j][i] = max(dp[j][i],dp[j][k]+dp[j+k][i-k]);
}
}
}
}
printf("%d\n",dp[][len] * );
}
return ;
}
poj2955括号匹配 区间DP的更多相关文章
- 括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
- poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- [poj2955/nyoj15]括号匹配(区间dp)
解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少 ...
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...
- HDU4632 Poj2955 括号匹配 整数划分 P1880 [NOI1995]石子合并 区间DP总结
题意:给定一个字符串 输出回文子序列的个数 一个字符也算一个回文 很明显的区间dp 就是要往区间小的压缩! #include<bits/stdc++.h> using namesp ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- TZOJ 3295 括号序列(区间DP)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
- POJ2955 Brackets (区间DP)
很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...
随机推荐
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- POJ3255Roadblocks[次短路]
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12697 Accepted: 4491 Descr ...
- using语法糖
资源的分类 首先说一下资源的分类: 托管资源:由CLR管理分配和释放资源 非托管资源:不受CLR管理的对象,如 文件(StreamReader,BinaryReader,DataTable,各种Str ...
- Jedis下的ShardedJedis(分布式)使用方法(一)
原来项目中有用到Redis用作缓存服务,刚开始时只用一台Redis就能够满足服务,随着项目的慢慢进行,发现一台满足不了现有的项目需求,因为Redis操作都是原子性这样的特性,造成有时同时读写缓存造成查 ...
- 机器学习实战--logistic回归
#encoding:utf-8 from numpy import * def loadDataSet(): #加载数据 dataMat = []; labelMat = [] fr = open(' ...
- mysql下mysqladmin日常管理命令总结
mysqladmin 工具的使用格式:mysqladmin [option] command [command option] command ......参数选项:-c number 自动运行次数统 ...
- oracle 事务总结
用了这么长时间的oracle,该总结一下所得了 1,事务 事务用于保证数据的一致性, 它由一组相关的 dml语句组成, 该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败,比 ...
- Anterior and posterior commissures
Source: https://en.wikipedia.org/wiki/Posterior_commissure Figrues archive.
- JS给文本框赋值后,在页面后台取不到文本框值的解决方法
转自:http://www.cnblogs.com/qiaohd/archive/2012/03/23/2413660.html (ReadOnly.disabled 都有可能造成取值取不到) 开发一 ...
- datahub
https://help.aliyun.com/document_detail/27854.html