POJ2955

匹配则加一,不需要初始化

 //#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
#define a first
#define b second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define debug1(a)\
cout << #a << " = " << a << endl;
#define debug2(a,b)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;
#define debug3(a,b,c) cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;
#define debug4(a,b,c,d)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;\
cout << #d << " = " << d << endl;
#define debug5(a,b,c,d,e)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;\
cout << #d << " = " << d << endl;\
cout << #e << " = " << e << endl;
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
using namespace std;
//**********************************
char s[];
int n;
int dp[][];
//**********************************
bool match(int a,int b)
{
char x=s[a],y=s[b];
return x=='['&&y==']'||x=='('&&y==')';
}
void solve()
{
FOR(len,,n)FOR(l,,n-len+){
int r=l+len-;
dp[l][r]=dp[l+][r-]+match(l,r);
rep(k,l,r){
dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+][r]);
}
}
printf("%d\n",dp[][n]<<);
}
//**********************************
int main()
{
while(~scanf("%s",s+)&&s[]!='e'){
n=strlen(s+);
solve();
}
return ;
}

CF

不匹配加一,注意初始化边界

 //#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define debug1(a)\
cout << #a << " = " << a << endl
#define debug2(a,b)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;
#define debug3(a,b,c) cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;
#define debug4(a,b,c,d)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;\
cout << #d << " = " << d << endl;
#define debug5(a,b,c,d,e)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;\
cout << #d << " = " << d << endl;\
cout << #e << " = " << e << endl
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
using namespace std;
//**********************************
int a[];
int n;
int dp[][];
//**********************************
bool match(int x,int y)
{
return a[x]==a[y];
}
void solve()
{
cl(dp,INF);
FOR(i,,n){
dp[i][i]=;
if(a[i]==a[i+])dp[i][i+]=;
}
FOR(len,,n){
FOR(l,,n-len+){
int r=l+len-;
if(match(l,r)){
if(r==l+)dp[l][r]=;
else dp[l][r]=dp[l+][r-];
}
// if(match(l,r))dp[l][r]=min(dp[l][r],dp[l+1][r-1]);
rep(k,l,r){
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+][r]);
}
}
}
printf("%d\n",dp[][n]);
}
//**********************************
int main()
{
cin>>n;
FOR(i,,n)cin>>a[i];
solve();
return ;
}

区间dp括号匹配的更多相关文章

  1. 区间dp 括号匹配问题

    这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...

  2. 区间dp - 括号匹配并输出方案

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  3. poj 2955 Brackets (区间dp 括号匹配)

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

  4. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  5. poj2955括号匹配 区间DP

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

  6. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  7. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  8. [NYIST15]括号匹配(二)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...

  9. NYOJ 题目15 括号匹配(二)(区间DP)

    点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...

随机推荐

  1. K2 BPM_快消零售连锁行业门店选址解决方案_十年专注业务流程管理系统

    >>>业务流程管理软件选型攻略  快消零售连锁行业门店选址解决方案   业内有句名言:“门店最重要的是什么?第一是选址,第二是选址,第三还是选址” 选址是一个很复杂的综合性商业决策过 ...

  2. linux使用glibc版本安装mysql8.0.12

    1.前言 使用yum安装虽然很方便,但是如果要是在没有公网的环境下,是没有办法使用yum源的.所以我们可以使用mysql提供的glibc版本的安装包,进行安装. 但是在安装之前,一定要将以前的版本删除 ...

  3. log4j日志properties配置

    #Console Log log4j.rootLogger=INFO,console,debug,info,warn,error LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss ...

  4. 【枚举】【lrj黑书】奇怪的问题(古老的智力题)

    题目描述: 请回答下面的 10 个问题,你的回答应保证每题惟有你的选择是正确的. ⑴ 第一个答案是b 的问题是哪一个?(a )2 ( b ) 3 ( c ) 4 ( d ) 5 ( e ) 6⑵ 恰好 ...

  5. 【javascript】h5页面禁止返回上一页

    window.history.pushState("","","#"); window.addEventListener("pop ...

  6. ubuntu---解决pip安装tf很慢的问题

    ubuntu---解决pip安装tf很慢的问题 [问题] 执行 u@u160406:~$ sudo pip3 install tensorflow-gpu==1.15.0rc2 下载速度真的很慢 Lo ...

  7. zznu-oj-2117 : 我已经在路上了(求函数的原函数的字符串)--【暴力模拟题,花式模拟题,String大法好】

    2117 : 我已经在路上了 时间限制:1 Sec 内存限制:256 MiB提交:39 答案正确:8 提交 状态 编辑 讨论区 题目描述 spring是不折不扣的学霸,那可是机房考研中的头号选手,不吹 ...

  8. 希尔排序Shell_Sort

    概述:听到希尔排序这个名称,心里完全没有任何概念,因为这个名称不能给你提供任何有效的信息.但是它的名字又是那么的特殊,以至于学习过数据结构排序的都知道这种方法的存在.现在我们就来看一下所谓的希尔排序. ...

  9. easyui-dialog打开之后append("标签")标签存在但是显示不出来

    初始化dialog $("#upDiv").dialog("open");//初始化dialog弹出窗口 注意: 1:  append("标签&quo ...

  10. Python中的if和while

    if 判断 if形式有三种: 1.if ... 2.if ... else ... 3.if ... elif ... else ... 实例: inp = raw_input('>>&g ...