Week12 作业 D - 选做题(括号匹配子序列)

Vcenter虚拟机磁盘转换实战:如何用StarWind V2V将vmdk转为qcow2(附空间优化技巧) 本文详细介绍了如何使用StarWind V2V Converter将Vcenter中的vmdk虚拟机磁盘高效转换为qcow2格式,并提供了空间优化技巧。从准备工作、导出vmdk文件到转换过程中的性能调优,帮助管理员在迁移虚拟机时减少资源消耗,确保数据完整性和性能。 阅读详情

一、题目描述

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 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                         

二、思路概述

  • 题意为,给定一个括号序列,找到它的长度最大的合法的子序列(合法表示这个序列中的括号都相互对应)

  • dp[i][j]表示子序列[i,j]变成合法序列需要添加的最少括号的数量。
    假如第 i 个和第 j 个是一对匹配的括号那么 dp [ i ] [ j ] = dp [ i+1 ] [ j-1 ] + 2 ;
    状态转移方程为dp[ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ ff ] + dp [ ff+1 ] [ j ] ) 。

  • dp[i][j]的大小,可看是它原来的值比较大,还是[i,j]之间取个断点,两段的和比较大,用大的值进行更新。

三、完整代码

/* f[i][j]表示子序列[i,j]变成合法序列需要添加的最少括号的数量。 
 假如第 i 个和第 j 个是一对匹配的括号那么 dp [ i ] [ j ] = dp [ i+1 ] [ j-1 ] + 2 ;
 dp[ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ ff ] + dp [ ff+1 ] [ j ] ) 。
*/
 
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;

int f[110][110];
string str;

int main(){
	
	while(1){
		
		cin>>str;if(str.compare("end")==0)break;
		memset(f,0,sizeof(f));
		
		int j,k;
		for(int i=1;i<str.length() ;i++){ 
			for(j=0,k=i;k<str.length();k++,j++){
				
				if(str[j]=='('&&str[k]==')')f[j][k]=f[j+1][k-1]+2;
				else if(str[j]=='['&&str[k]==']')f[j][k]=f[j+1][k-1]+2;
				
				for(int ff=j;ff<k;ff++){
					f[j][k]=max(f[j][k],f[j][ff]+f[ff+1][k]);
				}
			}
		}
		cout<<f[0][str.length()-1]<<endl;
		
	}
	return 0;
} 
SDM660电源部分配置: SDM660电源部分配置: Panel power supplies 1、Reference power supplies Description Name Regulator Voltage (V) Panel power supply 0 vddio PMIC 660/L11 1.8 Panel power supply 1 Lab PMIC 660/lab 4.6 Panel power s... 阅读详情

相关推荐

Vivado实战:AXI转PCIe IP核配置全流程(含性能优化技巧)

本文详细介绍了在Vivado环境中配置AXI转PCIe IP核的全流程,包括工程准备、IP核参数设置、BAR空间配置、中断系统优化以及TLP传输效率提升等关键步骤。特别分享了经过验证的性能优化技巧,帮助开发者显著提升PCIe链路的吞吐量和稳定性,适用于视频采集、高速数据记录等应用场景。

weixin_30892037的博客 557

Brackets(动态规划-区间dp)

Brackets(动态规划-区间dp) judge:poj 2955 source:Stanford Local 2004 Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 15405Accepted: 8061 Description We give the following inductive definition of a ...

画船听雨眠 326

用Arduino和FS-i6X遥控器,从零开始打造你的第一只仿生扑翼蝴蝶(附完整代码解析)

本文详细介绍了如何利用Arduino和FS-i6X遥控器从零开始制作仿生扑翼蝴蝶,包括硬件准备、电路连接、代码解析和调试技巧。通过完整的代码示例和参数说明,帮助读者快速掌握仿生机器人的制作要点,实现蝴蝶的自然飞行效果。

weixin_28727975的博客 320

week12-作业-动态规划(三)

文章目录D-选做题-1题目:Input:Output:Sample Input:Sample Output:题目分析:代码:E-选做题-2题目:Input:Output:Sample Input:Sample Output:Hint:题目分析:代码: D-选做题-1 题目: We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets

chihiro的博客 658

反序列化漏洞详解

目录 一、什么是序列化和反序列化 二、什么是反序列化漏洞 三、序列化函数(serialize) 四、反序列化(unserialize) ​五、什么是PHP魔术方法 六、一些常见的魔术方法 七、魔术方法的利用 八、反序列化漏洞的利用 1.__destruct()函数 2.__wakeup() 3.toString() ​九、反序列化漏洞的防御 一、什么是序列化和反序列化 序列化是将对象转换为字符串以便存储传输的一种方式。而反序列化恰好就是序列化的逆过程,反序列化会将字符串.

LJH1999ZN的博客 3万+

获取B站某用户更多的关注数和粉丝数

获取B站某用户更多的关注数和粉丝数 相关记录 一、 前言 B 站最多只能翻 5 页用户的关注数和粉丝数,如何能够看到更多呢。 方法我也是从网上翻来的,记载博客里,算是我研究过这个话题了。 二、需要的东西 关注数的 api https://api.bilibili.com/x/relation/followings?vmid=2884629&pn=1&ps=20&order=desc&jsonp=jsonp 关注者(粉丝)的 api https://api.bilibili.

Enderman_xiaohei的博客 4834

【HDU 5184】 Brackets (卡特兰数)

Brackets Problem Description 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 seq...

weixin_30340819的博客 208

Coze+剪映小助手实战:5分钟搞定视频草稿创建工作流(附常见错误排查)

本文详细介绍了如何利用Coze平台与剪映小助手,通过create_draft接口快速搭建自动化视频草稿创建工作流。文章从核心价值、零基础搭建步骤、运行验证到深度错误排查,手把手教你5分钟内实现视频项目参数预设的标准化与自动化,显著提升短视频创作与团队协作效率。

oil88的博客 550

【Week 12 作业】D

题目描述 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 s

应是天仙狂醉,乱把白云揉碎 387

生产级项目实战,从0到1上线一个企业级AI客服系统(全流程)

写了59篇,从Agent是什么讲到现在,工具、RAG、记忆、LangChain、LangGraph、多智能体、部署、监控、成本控制全都过了一遍。知识散着学容易忘,最后这一篇把它们全揉进一个真实项目里,从需求分析到架构设计到代码实现到部署上线,完整走一遍。这个项目是一个企业级AI客服系统。我选这个场景是因为它最能体现Agent的综合能力,要用到RAG知识库、多轮对话记忆、工具调用、安全防护,还要考虑部署和运维。客服也是Agent落地最成熟的场景,能跑起来就有实际价值。先搞清楚要做什么。跟业务方聊了一圈,客服场

白话机器学习 143

大数据架构中的数据治理:元数据管理与数据血缘追踪实践

本文将聚焦大数据架构中数据治理的两大核心支柱——元数据管理与数据血缘追踪元数据管理的目标、类型与实施步骤(从需求分析到工具落地);数据血缘追踪的技术实现(静态解析、动态捕获、混合方案)与应用场景;如何将两者结合,构建完整的数据治理体系,并通过真实案例展示落地效果。元数据管理:定义了业务/技术/操作元数据,讲解了工具选型(如Apache Atlas)、采集(Hook/API)、存储(关系库+图数据库)、应用(数据发现、质量监控);数据血缘追踪。

欢迎来到我的CSDN空间!这里聚焦AI大模型应用实战,分享前沿技术、实战案例与开发经验。 871

区间DP 找出最大匹配括号数

题意: 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 se

txzhangZz的博客 348

YOLOv11改进 ,YOLOv11改进主干网络为MobileNetV3,助力涨点

MobileNetV3通过结合硬件感知网络架构搜索(NAS)和NetAdapt算法,通过新颖的架构改进进一步提升了性能。本文开始探讨了自动化搜索算法与网络设计如何协同工作,以利用互补方法来提升整体技术水平。通过这一过程,创建了两个新的MobileNet模型:MobileNetV3-Large和MobileNetV3-Small,分别针对高资源和低资源使用场景。这些模型随后被适配并应用于目标检测和语义分割任务。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1945

week12 作业D选做题1

选做题 - 1 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 bracke

qq_43738677的博客 329

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Week12 作业 D - 选做题 - 1

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 sequen

qq_32205311的博客 286

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

选做题-1 Week12作业D题

题目: 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 se

qq_45189704的博客 242

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【Week12作业 D】选做题-1【区间dp】

题意: 括号序列问题:定义一个合法的括号序列: 1.空序列合法。 2.S合法,则(S)和 [S] 合法。 3.假设A和B合法,那么AB合法。 合法序列-(),[],(()),([]),()[],()[()]等。 非法序列-(,[,],)(等。 给定一个序列,求它最长的合法序列长度。 思路: 令f[i][j]表示子序列[i,j]的最长合法序列的括号对数 。 1.f[i][j]=max{f[i][k]+f[k+1][j],(i<=k<j)}对以下取max : 2.[S’]或(S’):f[i+1][

xKuro的博客 309

Week12 作业选做题(区间dp,状压dp)

D: Sample Input ((())) ()()() ([]]) )[)( ([][][) end Sample Output 6 6 4 0 6 简化一下题意: 要找由[(])四个字符组成的字符串的最长子序列,这个子序列必须满足,所有的括号里都不能包含单数个另一种括号的一边(这一边无法再匹配到另一半) 那么,这道题就成为了区间DP的典型题。 刚开始遍历左右括号之间距离为0的情况,再遍历距离为1,2…n的情况。按顺序从左到右遍历,根据状态转移方程: j=i+len; if((s[i]=='('&a

LeeviFang的博客 282

[week12] D - 选做题 —— 动态规划(区间DP)

文章目录题意InputOutput输入样例输出样例提示分析总结代码 题意 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 i

明哲天翊的博客 489

程序设计思维与实践 Week12 作业 D 选做题 - 1

We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, ifsis a regular brackets sequence, then (s) and [s] are regular brackets sequences, and ifaandbare regular brackets seq...

cax1165 323

程序设计思维与实践 Week12 作业 选做题 D-合法括号

题目链接:D-合法括号 题目描述: 我们给出了“规则括号”序列的归纳定义: 空序列是一个规则括号序列, 如果s是一个规则括号序列,那么(s)和[s]是规则括号序列, 如果a和b是规则括号序列,那么AB型是一个规则括号序列。 没有其他序列是规则括号序列。 例如,以下所有字符序列都是规则括号序列: (), [], (()), ()[], ()[()] 而下列字符序列不是: (, ], )(, ([)], ([(] 给出一个括号序列的字符a1a2 … an,您的目标是找到最长的规则括号序列的长度,这是s。也就是说

qq_43666020的博客 362

week12作业D

dp[i][j]表示区间[i,j]中符合要求的最长字串长度。 首先,如果区间长度小于等于1,那符合条件的长度肯定是0;如果区间长度为2,只有()和[ ]符合,长度为2,否则是0。 区间大于2的时候,如果区间边界str[i]=’(‘并且str[j]=’)‘或者str[i]=’[‘并且str[j]=’]’,那么dp[i][j]=dp[i+1][j-1]+2。 但是区间大于2的时候可能不符上述条件,对于[s’,s’],(s’或者s’)的情况,s’表示符合条件的子串,dp[i][j]=max(dp[i+1][j],

qq_45639151的博客 241

Week 12 D

选做题 - 1 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 bracket

weixin_44203022的博客 134

【Week12作业】D题

D题 题意: 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

【Week11 作业】必做题 209

Week12 选做部分

D - 选做题 - 1 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, andif a and b are regular bra

Miracle的博客 147

【Week 12 作业】A - 必做题 - 1、B - 必做题 - 2、C - 必做题 - 3、D - 选做题 - 1、E - 选做题 - 2

A - 必做题 - 1 题意: 给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少? Input 本题包含多组数据: 每组数据包含两行。 第一行一个数字N(1<=N<=999999) ,保证N为奇数。 第二行为N个用空格隔开的整数。 数据以EOF结束。 Output 对于每一组数据,你需要输出你找到的唯一的数。 Sample Input 5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1 Sample O

weixin_44898140的博客 379
上一篇: Week12 作业 C - 必做题(n个数里取m段使它们的和最大)
下一篇: Week12 作业 E - 选做题(ddl扣分)
冰瓜拿铁
博客等级 码龄8年 29粉丝 78原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址