题目:

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

思路:

区间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. ASP.NET项目开发实战<<一键创建解决方案>>

    视频演示地址:http://www.youku.com/playlist_show/id_23192838.html 第一步:创建项目需要的数据库 打开辅助开发工具,如下图 从左侧菜单找到 项目数据库 ...

  2. Hibernate对集合属性的操作---基础学习

    1:Set集合属性操作 1).Hibernate3以后支持大部分重要的JDK集合接口映射,Set集合接口的配置:  >在xxx.hbm.xml文件中使用<set>标签 2).< ...

  3. 洛谷 P3358 最长k可重区间集问题 【最大费用最大流】

    同 poj 3680 https:www.cnblogs.com/lokiii/p/8413139.html #include<iostream> #include<cstdio&g ...

  4. 洛谷 P3708 koishi的数学题

    找规律发现\( f[i]=f[i-1]+n-\sum_{i的因数和} \) 一A了深(sh)蓝(ui)题的我被找规律绿题卡死 记得开long long #include<iostream> ...

  5. 【转】Postman 使用方法详解

    1.Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介 ...

  6. 进击的Python【第十六章】:Web前端基础之jQuery

    进击的Python[第十六章]:Web前端基础之jQuery 一.什么是 jQuery ? jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多&quo ...

  7. 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows || 凸包模板

    整篇都是仅做记录... 蓝书上的板子.水平序,单调栈.先求下凸包,再求上凸包.叉积的作用是判定向量的位置关系. 48行的作用是在求上凸包的时候不至于去删下凸包中的点.上凸包中第一个点被认为是t1. 另 ...

  8. magento controller直接渲染Block 以及传参

    class Jago_Deal_IndexController extends Mage_Core_Controller_Front_Action { public function ajaxActi ...

  9. 用java打印输出九九乘法表

    package com.wh.multiplication public class Multiplication Table { public static void main(String[] a ...

  10. linux安装glassfish并布署

    1 https://glassfish.java.net/download.html 2 准备工作:需要jdk7以上版本 Java EE 7 requires JDK 7 (or above) 下载g ...