POJ 2955 Brackets(括号匹配一)】的更多相关文章

题目链接: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…
题意:最多有多少括号匹配 思路:区间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]=…
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…
题目链接: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…
题目链接: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…
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[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<…
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…
//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(…