poj 2955 Brackets 括号匹配 区间dp】的更多相关文章

题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+2), a[k]与a[j] 匹配,结果一组数据出错 ([]]) 检查的时候发现dp[2][3]==2,对,dp[2][4]=4,错了,简单模拟了一下发现,dp[2][4]=dp[2][1]+dp[2][3]+2==4,错了 此时2与4已经匹配,2与3已经无法再匹配. 故转移方程改为dp[i][j]=…
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方程: if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){ dp[i][j]=dp[i+1][j-1]+1; }dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]) ,(i<=k…
Brackets 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 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Description 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 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5424   Accepted: 2909 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular…
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来.如:[]是匹配的([])[]是匹配的((]是不匹配的([)]是不匹配的   输入 第一行输入一个正整数N,表示测试数据组数(N<=10)每组测试数据都只有一行,是一个字符串S,S中只包含以上所说的四种字符,S的长度不超过100 输出 对于每组测试数据都输出一个正整数,表示最少需要添加的括号的数量.每组…
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular…
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp[i][j]表示第i个到第j个需要添加的最少的括号,pos[i][j] = k;表示i到j间第k个需要加括号: 如果str[i]和str[j]匹配,那么dp[i][j] = max(dp[i + 1][j - 1], dp[i][j]); 如果str[i]和str[j]不匹配,那么dp[i][j]…
解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少的括号数量,用总数减去$dp[0][s.size()-1]$即可. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #i…
题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列如下: 1 空序列是一个合法的序列 2 如果S是合法的序列,则(S)和[S]也是合法的序列 3 如果A和B是合法的序列,则AB也是合法的序列 例如:下面的都是合法的括号序列 (),  [],  (()),  ([]),  ()[],  ()[()] 下面的都是非法的括号序列 (,  [,  ),  …
题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[i][j]代表从区间i到区间j所匹配的括号的最大个数,首先,假设不匹配,那么dp[i][j]=dp[i+1][j]:然后查找i+1~~j有木有与第i个括号匹配的 有的话,dp[i][j]=max(dp[i][j],dp[i+1][k-1]+dp[k][j]+2)..... #include<cstd…
Description 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 regul…
Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7795   Accepted: 4136 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular…
题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: //#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #define MAX 1000000000 #define FRE() freopen(&qu…
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…
Description 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 regul…
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ],求最大匹配? 题解: 定义dp[ i ][ j ] : 从第i个字符到第j个字符的最大匹配. 步骤: (1) : 如果s[ i ] 与 s[ j ]匹配,那么dp[ i ][ j ] =  2+dp[ i+1 ][ j-1 ];反之,dp[ i ][ j ] = 0; (2) : 接下来,从 i 到…
题意:问最多有几个括号匹配 思路:用dp[i][j]表示i到j最多匹配,若i和j构成匹配,那么dp[i][j] = dp[i + 1][j - 1] + 2,剩下情况dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]) 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<…
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory limit : 32 M Submitted : 188, Accepted : 113 5.1 Description We give the following inductive definition of a "regular brackets" sequence: • the empt…
题意: 给出一个字符串,其中仅仅含 “ ( ) [ ] ” 这4钟符号,问最长的合法符号序列有多长?(必须合法的配对,不能混搭) 思路: 区间DP的常规问题吧,还是枚举区间[i->j]再枚举其中第k个与第i个来配对,如果配对了就+2这样子. //#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <cmath>…
话说这题自己折腾好久还是没有推出转移的公式来啊------------------ 只想出了dp[i][j]表示i到j的最大括号匹配的数目--ค(TㅅT)------------------- 后来搜题解看到有两种有一点点不同的做法 dp[i][j] = max(dp[i+1][j-1] + ok(i,j), dp[i][k] + dp[k+1][j]) #include<cstdio> #include<cstring> #include<iostream> #inc…
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int n; int rec(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return dp[l][r]=0; if(l+1==r){ if(s[l]=='('&&s[r]==')') return dp[l][r]=2; if(…
题目传送门(洛谷)   题目传送门(UVA) 解题思路 很显然是一个区间dp,当然记忆化搜索完全可以AC,这里说一下区间dp. 区间dp的重要特征就是需要枚举中间节点k 看一看这道题,用f[i][j]表示从i...j组成合法序列需要添加括号的个数, 很显然,当s[i]==s[j]时,f[i][j]=f[i+1][j-1],然后枚举中间点k,就能写出动态转移方程:f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]) 为了保证在求f[i][j]时f[i+1][j-1].f[i…
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: 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, th…
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[()]”,都是规则的序列.这几个不是规则的,如:“(”.“[”.“]”.“)(”.“([()”. 输入 输入有多组测试数据.输入一串字符串序列,长度不大于255. 输出 输出最少添加的括号数目. 样例输入 ()(([()[[(([] 样例输出 0124题意 如上 题解 DP[i][j]代表区间[i,j…
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[…
题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最大和. 解题思路:有两种写法: ①这是我一开始想的,从外推到内,设立数组dp[i][j]表示剩下i~j时的最优解,则有状态转移方程: dp[i][j]=dp[i][j]=max(dp[i-1][j]+a[i-1]*(n-(j-i+1)),dp[i][j+1]+a[j+1]*(n-(j+1-i)))…
题目背景 给定一个正整数序列a(1),a(2),...,a(n),(1<=n<=20) 不改变序列中每个元素在序列中的位置,把它们相加,并用括号记每次加法所得的和,称为中间和. 例如: 给出序列是4,1,2,3. 第一种添括号方法: ((4+1)+(2+3))=((5)+(5))=(10) 有三个中间和是5,5,10,它们之和为:5+5+10=20 第二种添括号方法 (4+((1+2)+3))=(4+((3)+3))=(4+(6))=(10) 中间和是3,6,10,它们之和为19. 题目描述…
Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consisting of characters "(" and ")". Sereja needs to answer m queries, each of them is described by two integers li, ri(1 ≤ li ≤ ri…
区间DP #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; ]; ][]; int main() { int i,j,k; while(~scanf(" %s",s)) { ) break; int len=strlen(s); ; i>=; i--) s[i+]=s[i]; memset(dp,,s…