问题描述

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 sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular
【输入】The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
【输出】
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
【样例输入】`

((()))
()()()
([]])
)[)(
([][][)
end

【样例输出】

6
6
4
0
6

问题分析

括号序列问题,属于区间DP问题,当我们求最少添加括号数时过程如下:
• 长度为1的串的答案一定是1,长度为0的串的答案一定是0。
• 对于某个子序列S = [si,sj]
• 1)f[i][ j]的答案可以由子问题更新而来,即
• f[i][ j] = min{f[i][k] + f[k+1][ j]},(i≤k<j)
• 2)若S形如[S’]或(S’) ,那么f[i][ j] = min{f[i+1][ j-1]}
• 3)若S形如[S’或(S’,那么f[i][ j] = min{f[i+1][ j] + 1}
• 4)若S形如S’]或S’),那么f[i][ j] = min{f[i][ j-1] + 1}
• 上述四种情况取min,得到的答案就是子序列的答案f[i][ j]
• 3,4情况包含于1。可以看作两种理解方式,即两端的括号既可以左右两端匹配。也可以理解为第一种情况的k=1或k=j-1时的情况。
但是,题目中求得的是最大匹配括号数因此本文题不能进行两段枚举,要进行单向枚举遍历,较长的区间从较短的区间转移,首先,在括号可匹配时 dp[i][j]=min(dp[i][j],dp[i+1][j-1]);之后遍历,对于任意大于1的序列均可划分为两部分 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);采用反向思想求得最大匹配括号数

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;int dp[110][110];
int n;
bool match(char a,char b)
{if((a=='('&&b==')')||(a=='['&&b==']'))return true;return false;
}
int main()
{string s;while(cin>>s){memset(dp,0,sizeof(dp));if(s=="end")    break;n=s.length();for(int i=0;i<=n;i++)dp[i][i]=1;for(int i=n-2;i>=0;i--){for(int j=i+1;j<n;j++){dp[i][j]=n;if(match(s[i],s[j]))dp[i][j]=min(dp[i][j],dp[i+1][j-1]);for(int k=i;k<j;k++)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);}}cout<<n-dp[0][n-1]<<endl;}return 0;
}

Week12—最大匹配括号数相关推荐

  1. 输出字符串中匹配最多的括号数

    [java]输出字符串中匹配最多的括号数 例如: 1: (()) 输出2: 2:((()))()(()) 输出3: public class Demo { public static void mai ...

  2. poj2955 Brackets 最大括号匹配 区间动态规划

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11633   Accepted: 6145 Descrip ...

  3. 动态规划训练9 [Brackets POJ - 2955 ]

    Brackets POJ - 2955 再明显不过的区间DP的题目了,要求求出给出符号式中最大匹配的括号数. 考虑区间[l,r],如果str[l]与str[r]匹配了,那么转移方程为dp[l][r] ...

  4. 递归/回溯:Generate Parentheses生成合法括号

    已知n组括号,开发一个程序,生成这n组括号所有的合法的组合可能. 例如:n = 3 结果为: ["((()))", "(()())", "(())() ...

  5. 软件工程概论个人作业02

    可怜的二柱子同学,老师又对他的自动出题系统提出了新的要求: 1.题目避免重复: 2.可定制(数量/打印方式): 3.可以控制下列参数: 是否有乘除法: 是否有括号(最多可以支持十个数参与计算): 数值 ...

  6. POJ 1068 Parencodings 模拟递归

    http://poj.org/problem?id=1068 题意:P=每个右括号前面的左括号,W=每个右括号所在的括号包含的完整括号数,包括其本身.已知P,求W. 模拟题,将原括号按照数据呈现出来, ...

  7. 人工智能实践:TensorFlow笔记学习(三)——TensorFlow框架

    搭建神经网络 大纲 3.1 张量.计算图.会话 3.2 前向传播 3.3 反向传播 目标 搭建神经网络,总结搭建八股 3.1 张量.计算图.会话 一.基本概念 基于Tensorflow的NN:用张量表 ...

  8. 将矩阵转为一行_矩阵与矩阵乘积简介

    作者|Hadrien Jean 编译|VK 来源|Towards Data Science 原文链接:https://towardsdatascience.com/introduction-to-ma ...

  9. python基础教程_学习笔记14:标准库:一些最爱——re

    标准库:一些最爱 re re模块包括对正則表達式的支持,由于以前系统学习过正則表達式,所以基础内容略过,直接看python对于正則表達式的支持. 正則表達式的学习,见<Mastering Reg ...

最新文章

  1. Javascript跨域后台设置拦截
  2. IOS设计模式学习(1)设计模式初窥
  3. java list 拼接 字符串数组_把数组所有元素排序,并按照“参数=参数值”的模式用“”字符拼接成字符串...
  4. 数据可视化的开源方案: Superset vs Redash vs Metabase (一)
  5. shell脚本详解(九)——一键部署DNS正向解析
  6. python中property函数_python 邮件表格Python中property函数用法实例分析
  7. LeetCode:安排工作以达到最大收益【455】
  8. X509证书基本概念
  9. Mac如何创建快捷方式?
  10. 你必须知道的 NET(第2版)
  11. php 虚拟ip 刷流量,浅析网站刷流量的利与弊
  12. 查看MySQL初始密码并修改
  13. python框架支持套接字么_Python的框架比较:Django,金字塔,水瓶,Sanic,旋风,BottlePy等等...
  14. Unity显示360度全景照片
  15. 程序员泪流满面的图片
  16. 使用孪生网络和零样本学习进行文本分类
  17. 韩服游戏IP用哪里的比较稳定怎么选择服务器
  18. web(ASP)常用代码
  19. MySQL设置白名单,允许单个IP或某段节点登录
  20. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;

热门文章

  1. 编写第一个 Arduino 程序
  2. php实现批量导出pdf 压缩打包下载
  3. Pytorch单机多卡加速
  4. VBA 模块级变量和过程级变量,全局变量,局部变量,end,exit end sub等影响
  5. 传说中的Q_PROPERTY怎么使用
  6. SpringBoot 文件管理微服务 支持FastDFS/FTP/阿里云存储、华为云存储/天翼云存储/联通云存储移动云存储
  7. NavicatforMySQL_繁星漫天_新浪博客
  8. 数据分析师需要学习什么软件
  9. Beyond compare用法详解
  10. android 表情的输入 Unicode实现表情展示 无需图片素材及相关解析