Brackets (区间DP)

个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对

对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越大时很多重复,应该要进行分割,

后面想想又不对,就去看题解了,没想到就是分割,还是动手能力太差,还有思维不够。

1 for(int j=0;j+i<ch.size();j++)
2                     {
3                     if(check(j,j+i))
4                         dp[j][j+i]=dp[j+1][j+i-1]+2;
5                         for(int m=j;m<=j+i;m++)
6                     dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+1][j+i]);
7                     }

分割并一次求最大值。动态规划真的是一脸懵逼样,多思考,多瞎想吧,呼~

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 i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

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.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

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

Sample Output

6
6
4
0
6
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iomanip>
 6 #include<string>
 7 #include<algorithm>
 8 using namespace std;
 9 int money[205];
10 int dp[205][205];
11 string ch;
12 const int inf=999999;
13 int check(int i,int j){
14     if((ch[i]=='('&&ch[j]==')')||(ch[i]=='['&&ch[j]==']'))
15         return 1;
16       return 0;
17 }
18 void init(){
19    for(int i=0;i<ch.size();i++)
20      for(int j=0;j<ch.size();j++)
21         dp[i][j]=0;
22 }
23 int main(){
24     int n,m;
25     while(getline(cin,ch,'\n')){
26             if(ch=="end") break;
27             init();
28             for(int k=0;k<ch.size()-1;k++)
29                if(check(k,k+1))
30                dp[k][k+1]=2;
31                else
32                 dp[k][k+1]=0;
33                 for(int i=2;i<ch.size();i++)
34                {
35                    for(int j=0;j+i<ch.size();j++)
36                     {
37                     if(check(j,j+i))
38                         dp[j][j+i]=dp[j+1][j+i-1]+2;
39                         for(int m=j;m<=j+i;m++)
40                     dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+1][j+i]);
41                     }
42 
43                }
44             cout<<dp[0][ch.size()-1]<<endl;
45     }
46     return 0;
47 }

 



转载于:https://www.cnblogs.com/blvt/p/7371994.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371482.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android生成aar无效,android studio生成aar包并在其他工程引用aar包的方法

1.aar包是android studio下打包android工程中src、res、lib后生成的aar文件&#xff0c;aar包导入其他android studio 工程后&#xff0c;其他工程可以方便引用源码和资源文件2.生成aar包步骤&#xff1a;①.用android studio打开一个工程&#xff0c;然后新建一个Module&#…

《剑指offer》— JavaScript(3)从尾到头打印链表

从尾到头打印链表 题目描述 输入一个链表&#xff0c;从尾到头打印链表每个节点的值。 实现代码 /*function ListNode(x){this.val x;this.next null; }*/ function printListFromTailToHead(head) {var res[];while(head){res.unshift(head.val);headhead.next;}return res;…

JUnit测试Spring Service和DAO(带有内存数据库)

这篇文章描述了如何为Spring Web Application的Services和DAO实现JUnit测试。 它建立在Spring MVC-Service-DAO-Persistence Architecture Example的基础上 。 从Github的Spring-Web-JPA-Testing目录中可以找到该示例。 提醒 测试装置 –固定状态&#xff0c;用作运行测试的基…

c# 正则获取html标签内容,c# – 使用正则表达式在多个HTML标记之间获取文本

使用正则表达式,我希望能够在多个DIV标记之间获取文本.例如,以下内容&#xff1a;first html taganother tag输出&#xff1a;first html taganother tag我使用的正则表达式模式只匹配我的最后一个div标签并错过了第一个.码&#xff1a;static void Main(string[] args){string…

Android之外部存储(SD卡)

*手机的外部存储空间&#xff0c;这个我们可以理解成电脑的外接移动硬盘&#xff0c;U盘也行。所有的Android设备都有两个文件存储区域&#xff1a;“内部”和“外部”存储器。这两个名称来自早期的Android&#xff0c;当时大多数设备都提供内置的固定的内存&#xff08;内置存…

通用并发对象池

在本文中&#xff0c;我们将介绍如何在Java中创建对象池。 近年来&#xff0c;JVM的性能成倍增加&#xff0c;大多数类型的对象几乎都变得多余&#xff0c;从而提高了对象池的性能。 从本质上讲&#xff0c;对象的创建不再像以前那样昂贵。 但是&#xff0c;有些对象在创建时肯…

圆周率的代码表示,以及对其的理解。

转载的简书&#xff0c;for 记录以及记忆。 http://www.jianshu.com/p/7208e4a58310 Thanks again&#xff01; 转载于:https://www.cnblogs.com/xiapeng0701/p/7538281.html

华为NOVa8Pr0是用鸿蒙系统吗,华为Nova8即将发布,采用麒麟芯片,高端平板适配鸿蒙系统...

大家好&#xff0c;我是老孙自从华为Mate40系列发布后&#xff0c;下一步新机动态备受外界关注&#xff0c;华为究竟会不会继续生产手机呢&#xff1f;答案是肯定&#xff0c;华为Nova8系列将于本月发布&#xff0c;华为P50系列也在积极筹备&#xff0c;而且都少不了麒麟芯片&a…

使用路标的Scala和Java的Twitter REST API

如果您已阅读此博客上的其他文章&#xff0c;您可能会知道我喜欢创建各种数据集的可视化。 我刚刚开始一个小项目&#xff0c;在这里我想可视化来自Twitter的一些数据。 为此&#xff0c;我想直接从Twitter检索有关关注者的信息和个人资料信息。 我实际上开始寻找一组所有推特帐…

大话设计模式读书笔记--11.抽象工厂模式

定义 抽象工厂模式定义: 提供一个创建一系列相关或相关依赖对象的接口,而无需指定他们具体的类 抽象工厂模式通常是用于创建一族产品&#xff0c;并且这族产品分不同的等级&#xff1b;不同的具体工厂类生产不同等级的一族产品 比如下图(来源于网络) 两厢车和三厢车称为两个不同…

在线压缩html,JS代码压缩 - javascript代码压缩 - jsmin在线js压缩工具

输入代码&#xff1a;// is.js// (c) 2001 Douglas Crockford// 2001 June 3// The -is- object is used to identify the browser. Every browser edition// identifies itself, but there is no standard way of doing it, and some of// the identification is deceptive. T…

Primefaces dataTable设置某个cell的样式问题

设置primefaces dataTable的源网段列的Cell可以编辑&#xff0c;当回车键保存时&#xff0c;判断是否输入的网段合法&#xff0c;如果不合法就显示警告信息&#xff0c;并将这个不合法的数据用红色表示。问题是&#xff0c;怎么给这一个cell设定样式。通过给标签设定ID然后在后…

鸭子在Java中打字? 好吧,不完全是

根据维基百科&#xff0c;鸭子的打字是&#xff1a; 动态类型的类型&#xff0c;其中对象的方法和属性确定有效的语义&#xff0c;而不是其从特定类或特定接口的实现继承 用简单的话 当我看到一只鸟走路像鸭子&#xff0c;游泳像鸭子&#xff0c;嘎嘎像鸭子一样时&#xff0c…

前端学习路线

第一部分 HTML 第一章 职业规划和前景 职业方向规划定位&#xff1a; web前端开发工程师 web网站架构师 自己创业 转岗管理或其他 web前端开发的前景展望&#xff1a; 未来IT行业企业需求最多的人才 结合最新的html5抢占移动端的市场 自己创业做老板 随着互联网的普及we…

p1164【立方体求和】

题目&#xff1a; SubRaY有一天得到一块西瓜,是长方体形的....SubRaY发现这块西瓜长m厘米,宽n厘米,高h厘米.他发现如果把这块西瓜平均地分成m*n*h块1立方厘米的小正方体,那么每一小块都会有一个营养值(可能为负,因为西瓜是有可能坏掉的,但是绝对值不超过200).现在SubRaY决定从这…

html生成自定义表格,自定义js的表格插件

场景&#xff1a;指定元素&#xff0c;生成自定义表格。目的&#xff1a;了解js的插件开发。html代码&#xff1a;自定义表格插件var test new MyTable({elid:"mytable",//定义哪个div要生成表单thead:{//指定列名name:"姓名",age:"年龄",addr:…

使用JBehave,Gradle和Jenkins的行为驱动开发(BDD)

行为驱动开发 &#xff08;BDD&#xff09;是一个协作过程 &#xff0c;产品负责人&#xff0c;开发人员和测试人员可以合作交付可为企业带来价值的软件。 BDD是 测试驱动开发 &#xff08;TDD&#xff09; 的合理下一步 。 行为驱动的发展 本质上&#xff0c;BDD是一种交付…

手型显示html,css各种手型集合(css禁止手型)

html>css各种手型集合(css禁止手型).auto { cursor: auto; }.deafult { cursor: default; }.none { cursor: none; }.context-menu { cursor: context-menu; }.help { cursor: help; }.pointer { cursor: pointer; }.p…

Maven Fluido Skin和Javadoc类图

我使用Maven网站已有一段时间了&#xff0c;对此我感到非常满意。 我不想在Maven 3之后更新我的项目&#xff0c;但是没关系&#xff0c;Maven 3带来了许多新奇的东西。 但是&#xff0c;有两件事使我感到烦恼&#xff1a;缺乏美观和现代的外观&#xff0c;以及浏览复杂代码的J…

ZooKeeper安装,部署

实验环境 192.168.1.10  Zookeeper1:2181, Zookeeper2:2182 192.168.1.11  ZooKeeper3:2181 依赖环境 JDK1.7 安装&#xff0c;配置 1、下载解压 # 192.168.1.10cd /data/server tar -zxv -f zookeeper-3.4.6.tar.gz ln -s zookeeper-3.4.6 zookeeper1ln -s zookeeper-3.4.…