poj 2955 Brackets
题目链接: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<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
#include<queue>
#include<iterator>
#include<vector>
#include<set>
#define dinf 0x3f3f3f3f
typedef long long ll; using namespace std; char s[];
int dp[][]; int main()
{
while(scanf("%s",s+) && s[]!='e')
{
s[]=; //以下3行注意顺序
int len=strlen(s);
len--;
memset(dp,,sizeof(dp));
for(int i=len-;i>=;i--)
{
for(int j=i+;j<=len;j++)
{
dp[i][j]=dp[i+][j];
for(int k=i+;k<=j;k++)
if((s[i]=='(' && s[k]==')') || (s[i]=='[' && s[k]==']'))
dp[i][j]=max(dp[i][j],dp[i+][k-]+dp[k][j]+);
}
}
printf("%d\n",dp[][len]);
}
return ;
}
poj 2955 Brackets的更多相关文章
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
- POJ - 2955 Brackets括号匹配(区间dp)
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- POJ 2955 Brackets (区间dp入门)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
随机推荐
- js实现由分隔栏决定两侧div的大小—js动态分割div
/*================index.html===================*/ <!DOCTYPE html><html lang="zh-cn&quo ...
- [转]struts2处理.do后缀的请求
原文地址:http://skyuck.iteye.com/blog/545988 默认情况下,struts2是无法处理以.do为后缀的请求url的(默认情况下是.action或者不填,可以参见org. ...
- mysql返回最后一列数据
获取MySQL的表中每个userid最后一条记录的方法,并且针对userid不唯一的情况,需要的朋友可以参考下 表结构 CREATE TABLE `t1` ( `userid` int(11) DEF ...
- 为网站文字前面添加图标 在线调用 Font Awesome 字体icon小图标 美化网站
一.如何开始 1.将下面的代码复制粘贴到HTML页面面的 <head> 下面 <link rel="stylesheet" href="https:// ...
- bzoj 1537: [POI2005]Aut- The Bus 线段树
bzoj 1537: [POI2005]Aut- The Bus 先把坐标离散化 设f[i][j]表示从(1,1)走到(i,j)的最优解 这样直接dp::: f[i][j] = max{f[i-1][ ...
- Map工具系列-01-Map代码生成工具说明
所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...
- 获取CPU信息
1 查看手机CPU信息 cmd——adb shell——cd /proc------cat cpuinfo 2 获取cpu的是arm指令集,armv7指令集.还是neon指令集 /** * * [获取 ...
- marquee标签,好神奇啊...
<html><body><div style="height:190; margin-top:10; margin-bottom:10; width:96%; ...
- [NHibernate]HQL查询
目录 写在前面 文档与系列文章 查询的几种方式 HQL查询 一个例子 总结 写在前面 上篇文章介绍了nhibernate在项目中的基本配置,包括数据库连接字符串的设置,映射文件的配置及需注意的地方,这 ...
- [NHibernate]延迟加载
目录 写在前面 文档与系列文章 延迟加载 一个例子 总结 写在前面 上篇文章介绍了多对多关系的关联查询的sql,HQL,Criteria查询的三种方式.本篇文章将介绍nhibernate中的延迟加载方 ...