区间dp括号匹配
匹配则加一,不需要初始化
//#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 ;
}
不匹配加一,注意初始化边界
//#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括号匹配的更多相关文章
- 区间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 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- poj2955括号匹配 区间DP
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5424 Accepted: 2909 Descript ...
- 括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
- poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
- [NYIST15]括号匹配(二)(区间dp)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...
- NYOJ 题目15 括号匹配(二)(区间DP)
点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...
随机推荐
- Win10安装PyQt5与Qt Designer
1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 会自动下载PyQt5以及sip并安装,因为PyQt5不再提供Qt Designer等工具,所以需要再安装pyqt5- ...
- Kafka 快速入门
Kafka Kafka 核心概念 什么是 Kafka Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.该项目的目标是为处理实时数据提供一个统一.高吞吐.低延迟 ...
- Java 面向对象(六)接口
一.接口 接口:是Java语言中一种引用类型,是方法的集合,如果说类的内部封装了成员变量.构造方法和成员方法,那么接口的内部主要就是封装了方法,包含抽象方法(JDK 7及以前),默认方法和静态方法(J ...
- flume 1.7在windows下的安装部署与测试运行
一.安装 安装java,配置环境变量. 安装flume,下载地址,下载后直接解压即可. 二.运行 创建配置文件:在解压后的文件 apache-flume-1.7.0-bin\conf下创建一个exam ...
- js 面试题一
1.格式化数字,每三位加逗号 // 实现方式一 function formatNum(num){ var _num = num + ""; var result = "& ...
- 【Redis】事务 (超详细)
一.概述 二.相关命令列表 2.1 MULTI 2.2 EXEC 2.3 DISCARD 2.4 WATCH key [key ...] 2.5 UNWATCH 三.命令示例 3.1 事务被正常执行 ...
- MySQL在command line Client下的一些命令
MySQL在command line Client下的一些命令 通过CMD进入到本地数据库: mysql -h localhost -u -root -p 参数说明: -h 要连接的服务器的主机名或I ...
- 一个页面,WEB全功能
当鼠标在页面上往下滑动的时候,页面也一直向下,标签也顺带着全部向下滑动 以前浏览页面,主要是在PC上进行浏览,一个页面不适于太长,需要用户向下拖动: 当时的设计是,点击标签,点击不同的标签,跳转到不同 ...
- less嵌套规则
嵌套,是less里面最有意思的小东西,比如说我们经常性的去写一些列表性的东西 html <ul class="list"> <li><a href=& ...
- selenium 操作多个窗口间切换
#coding=gbk ''' selenium多个窗口间切换 ''' from selenium import webdriver as wd from selenium.webdriver imp ...