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. C# List<> Find相关接口学习

    参考 http://blog.csdn.net/daigualu/article/details/54315564 示例: List<int> test = new List<int ...

  2. js中prototype与__proto__的关系详解

    一.构造函数: 构造函数:通过new关键字可以用来创建特定类型的对象的函数.比如像Object和Array,两者属于内置的原生的构造函数,在运行时会自动的出现在执行环境中,可以直接使用.如下: var ...

  3. 表格中的DOM

    通过DOM来操作table跟在html中操作table是不一样的,下面来看看怎样通过DOM来操作table. 按照table的分布来创建: <table> <thead> &l ...

  4. 四、TreeSet

    HashSet 是无序的,如果要对集合实现排序,那么就需要使用TreeSet 让TreeSet 实现集合有序有两种方法 一.让元素自身具备比较排序功能,具备比较排序功能的元素只需要实现Comparab ...

  5. MSPBSL_Scripter编译

    The BSL Scripter is a PC application that is available for Windows, Linux and Mac OS X. It is a user ...

  6. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  7. selenium网页截图和截图定位(带界面)

    from selenium import webdriver import time from PIL import Image driver = webdriver.Chrome() driver. ...

  8. wsl2 debian安装docker

    应用商店下载debian 安装docker 安装依赖 打开安装好的docker安装依赖 sudo apt-get install apt-transport-https ca-certificates ...

  9. 查看SVN当前登录用户

    一般用户登录svn并记住用户密码后,下次再登录的时候将不需要输入用户密码,导致电脑使用着登录的时候,不知道到底登录的是个用户,只能将数据清除,现在给出查看登录用户的方法. 记录svn登录用户的文件,存 ...

  10. LeetCode--链表

    1.使用常量空间复杂度在O(n log n)时间内对链表进行排序. 思路: 因为题目要求复杂度为O(nlogn),故可以考虑归并排序的思想. 归并排序的一般步骤为: 1)将待排序数组(链表)取中点并一 ...