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. python 链接impala执行SQL

    项目中用python来链接impala数据库最常见,一个简单的链接,获取结果. #!/usr/bin/python # -*- coding:utf-8 -*- from impala.dbapi i ...

  2. 阿里高级架构师教你如何使用Spring Cloud Ribbon重试请求

    在微服务调用中,一些微服务圈可能调用失败,通过再次调用以达到系统稳定性效果,本文展示如何使用Ribbon和Spring Retry进行请求再次重试调用. 在Spring Cloud中,使用load b ...

  3. 学习CSRF漏洞并挖掘CSRF漏洞

    什么是跨站请求伪造? 跨站请求伪造(英语:Cross-siterequest forgery),也被称为one-clickattack或者session riding,通常缩写为CSRF或者XSRF, ...

  4. Endless looping of packets in TCP/IP networks (Routing Loops)

    How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...

  5. SignalR的三个Demo

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  6. YOLO---Darknet下的 GPU vs CPU 速度

    YOLO---Darknet下的 GPU vs CPU 速度 目录 一.基础环境 二.安装Darknet-yolo v3 三.CPU下测试 四.GPU下测试 五.测试速度对比结论 正文 一.基础环境 ...

  7. python调用cv2.findContours时报错:ValueError: not enough values to unpack (expected 3, got 2)

    OpenCV旧版,返回三个参数: im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_S ...

  8. 如何判断PHP空间是否支持curl、gzip等功能

    在网站根目录新建v.php,输入以下代码: <?php $f=@trim($_GET['f']); if(function_exists($f)) echo '支持'.$f; else echo ...

  9. [ 转载 ] vue.js面试题一

    转载自:https://www.cnblogs.com/aimeeblogs/p/9501490.html 如有侵权 联系删除 Vue.js面试题整理 一.什么是MVVM? MVVM是Model-Vi ...

  10. Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '<>'

    1.问题 今天又在mysql中遇到了,吐血. 2.解决方案 SQL最后加上 COLLATE utf8mb4_unicode_ci SELECT t2.cust_id as cust_id_ex,t1. ...