题目:

给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度。

思路:

区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解。还是做的少。

代码:

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 1000000000
#define FRE() freopen("in.txt","r",stdin) using namespace std;
const int maxn = ;
char str[maxn];
int dp[maxn][maxn]; int main()
{
//FRE();
while(gets(str))
{
if(strcmp(str,"end")==) { break; }
if(strcmp(str,"")==) {printf("0\n"); break;}
int length = strlen(str);
for(int i=; i<length; i++)
{
for(int j=; j<length; j++)
{ dp[i][j] = ; }
}
//printf("length: %d\n",length);
for(int len=; len<length; len++)//枚举区间的长度
{
for(int i=; i+len<length; i++)
{
int j = i+len;
if((str[i]=='(' && str[j]==')') || (str[i]=='['&&str[j]==']'))
{
dp[i][j] = max(dp[i+][j-]+,dp[i][j]);//当前这个区间是由哪个区间得来的
}
for(int k=i; k<=j; k++)//更新这个区间的最优解
{
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
// printf("%d\n",dp[1][2]);
// printf("%d\n",dp[3][4]);
// printf("%d\n",dp[1][4]);
// printf("%d\n",dp[1][5]);
printf("%d\n",dp[][length-]);
}
return ;
}

POJ - 2955 Brackets (区间DP)的更多相关文章

  1. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  4. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. POJ 2955 Brackets 区间DP 入门

    dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...

  6. POJ 2955 Brackets(区间DP)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...

  7. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  8. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  9. A - Brackets POJ - 2955 (区间DP模板题)

    题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...

  10. POJ 2955 Brackets 区间合并

    输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp ...

随机推荐

  1. unsign 字段相减出现负数解决方法

    在项目中做数据统计的时候需要用到几个字段相减得到想要的值,但是因为字段都是无符号,相减出现mysql 错误 BINGINT UNSIGNED VALUE ..  在c语言中两个无符号相减值为负数,该值 ...

  2. Luogu P1073 最优贸易【最短路/建反图】 By cellur925

    题目传送门 这么经典的题目,还是看了lyd的题解....唉难过. 一句话题意:在一张点有全都的图上找一条从1到n的路径,存在两个点p,q(p<q),使val[q]-val[p]最大. 给出的图是 ...

  3. 洛谷 P1288 取数游戏II

    奇奇怪怪的游戏,不多写了 #include<cstdio> ]; int main() { int i; scanf("%d",&n); ;i<=n;i+ ...

  4. 题解报告:hdu 2059 龟兔赛跑

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击—— ...

  5. Android 线程池系列教程(4) 启动线程池中的线程和中止池中线程

    Running Code on a Thread Pool Thread 上一课   下一课 1.This lesson teaches you to Run a Runnable on a Thre ...

  6. jdk 1.8下 java ArrayList 添加元素解析

    转载请注明http://www.cnblogs.com/majianming/p/8006452.html 有人问我,java ArrayList底层是怎么实现的?我就回答数组,他再问我,那它是怎么实 ...

  7. div 弹出 居中

    function show_tc_conv(){var x=jq13(window).width()/2;var y=jq13(window).height()/2; var div_w=jq13(& ...

  8. PHP + ORACLE 远程连接数据库环境配置

    在ORACLE官网下载instantclient_11_2,放在D盘 把instantclient_11_2目录下的所有dll文件复制到C:\Windows\SysWOW64   和  D:\phpS ...

  9. 构建微服务开发环境5————安装Node.js

    [内容指引] 下载Node.js: Mac下安装Node.js: Windows下安装Node.js; 查看node和npm的版本. 一.下载Node.js 访问Node.js官网:https://n ...

  10. 将call/apply方法应用于其他对象上的几种方法

    在处理类数组中,发现了两种将数组方法应用于类数组的方法,现将call/apply的常用方式总结一下. 一.当做函数调用 function print_vars(var1,var2){ console. ...