Brackets (区间DP)
个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对
对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越大时很多重复,应该要进行分割,
后面想想又不对,就去看题解了,没想到就是分割,还是动手能力太差,还有思维不够。
for(int j=;j+i<ch.size();j++)
{
if(check(j,j+i))
dp[j][j+i]=dp[j+][j+i-]+;
for(int m=j;m<=j+i;m++)
dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+][j+i]);
}
分割并一次求最大值。动态规划真的是一脸懵逼样,多思考,多瞎想吧,呼~
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;
int money[];
int dp[][];
string ch;
const int inf=;
int check(int i,int j){
if((ch[i]=='('&&ch[j]==')')||(ch[i]=='['&&ch[j]==']'))
return ;
return ;
}
void init(){
for(int i=;i<ch.size();i++)
for(int j=;j<ch.size();j++)
dp[i][j]=;
}
int main(){
int n,m;
while(getline(cin,ch,'\n')){
if(ch=="end") break;
init();
for(int k=;k<ch.size()-;k++)
if(check(k,k+))
dp[k][k+]=;
else
dp[k][k+]=;
for(int i=;i<ch.size();i++)
{
for(int j=;j+i<ch.size();j++)
{
if(check(j,j+i))
dp[j][j+i]=dp[j+][j+i-]+;
for(int m=j;m<=j+i;m++)
dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+][j+i]);
} }
cout<<dp[][ch.size()-]<<endl;
}
return ;
}
Brackets (区间DP)的更多相关文章
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3624 Accepted: 1879 Descript ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ2955 Brackets —— 区间DP
题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- POJ 3468 A Simple Problem with Integers 【线段树】
题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...
- 教你在树莓派使用上RTC实时时钟,不用再担心断电后时间归零的问题,开机后自动同步RTC时钟!!!
准备工作:1.系统建议使用官方最新的镜像文件 2.RTC时钟模块板(I2C接口)建议使用DS1307时钟模块,或者RTC时钟模块RTC时钟模块: 大家知道arduino的电平是5V,树莓派是3.3V, ...
- jQuery之DOM操作大全
jQuery属性操作 获取元素属性的语法:attr(name) 例子:$("#img1").attr("src"); 设置元素单个属性的语法:attr(key, ...
- 20145230《JAVA程序设计》第2周学习总结
20145230 <Java程序设计>第2周学习总结 教材学习内容总结 本周我学习了<JAVA学习笔记>中的第三章内容,让我对JAVA有了进一步的了解.第三章主要是介绍JAVA ...
- pd.read_csv的header用法
默认Header = 0: In [3]: import pandas as pd In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_d ...
- C++中随机数的生成
1.随机数由生成器和分布器结合产生 生成器generator:能够产生离散的等可能分布数值 分布器distributions: 能够把generator产生的均匀分布值映射到其他常见分布,如均匀分布u ...
- 解决MySQL因不能创建 PID 导致无法启动的方法
问题描述 MySQL 启动报错信息如下: ? 1 2 Starting mysqld (via systemctl): Job for mysqld.service failed because t ...
- spring配置hibernate映射文件-------通配符
<!-- 这里一定要注意是使用spring的mappingLocations属性进行通配的 --> <property name="mappingLocation ...
- inline-block和同级的text-align问题
https://www.cnblogs.com/qjqcs/p/5551640.html margin:0 auto:是设置块标签在父级中居中对齐,是一种对齐方式.所以对于display:inline ...
- 最长k可重区间集
P3358 最长k可重区间集问题 P3357 最长k可重线段集问题 P3356 火星探险问题 P4012 深海机器人问题 P3355 骑士共存问题 P2754 [CTSC1999]家园 题目描述 ...