博客
关于我
poj2955——括号匹配+区间dp经典题
阅读量:656 次
发布时间:2019-03-15

本文共 2711 字,大约阅读时间需要 9 分钟。

题目链接:

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 < … < imn, 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

 

题目翻译:

‎我们给出了"常规括号"序列的以下归纳定义:‎

  • ‎空序列是一个常规括号序列,‎
  • ‎如果‎‎s‎‎是常规括号序列,则 (‎‎s‎‎) 和‎‎*‎‎s = 是常规括号序列,并且‎
  • ‎如果‎‎a‎‎和‎‎b‎‎是常规括号序列,则‎‎ab‎‎是常规括号序列。‎
  • ‎没有其他序列是常规括号序列‎

‎例如,以下所有字符序列都是常规括号序列:‎

(), [], (()), ()[], ()[()]

‎而以下字符序列不是:‎

(, ], )(, ([)], ([(]

‎给定一个字符的括号序列‎‎1‎‎a 2‎‎ ...‎‎a n‎‎,您的目标是找到最长的常规括号序列的长度,该括号序列是‎‎s‎‎的子序列。也就是说,您希望找到最大的 m,因此‎‎对于索引 i ‎‎1‎‎, ‎‎i‎‎2‎‎, ..., ‎‎i‎‎m‎‎其中 1 = ‎‎i‎‎1‎‎ < ‎‎i‎‎2‎‎ < ...< ‎‎i‎‎m‎‎ ‎‎ = ‎‎n‎‎, ‎‎a‎‎i‎‎1‎‎a‎‎i‎‎2‎‎ ‎‎ ...‎‎a‎‎m‎‎是一个常规括号序列。‎

‎给定初始序列,最长的常规括号子序列为 。‎([([]])][([])]

‎输入‎

‎输入测试文件将包含多个测试用例。每个输入测试用例由一行组成,仅包含字符 、 和 。每个输入测试的长度将在 1 到 100 之间,包括。文件结尾用包含单词"end"的行标记,不应进行处理。‎()[]

‎输出‎

‎对于每个输入情况,程序应在一行上打印尽可能长的常规括号子序列的长度。

 

这道题可以说是最最经典的区间dp题了,也是一道十分好的题。

状态dp[i][j]表示从i区间到j区间内的匹配括号数。

状态转移方程

if(i和j匹配)

      dp[i][j]=dp[i+1][j-1]+2

dp[i][j]=max{dp[i][k]+dp[k][j]}k为最优分割点

#include 
#include
#include
#include
using namespace std;int main(int argc, char** argv) { char str[1005]; int dp[105][105]; while(scanf("%s",str+1)!=EOF&&strcmp(str+1,"end")!=0) { int n=strlen(str+1); memset(dp,0,sizeof(dp)); for(int len=2;len<=n;len++){ for(int i = 1;i<=n;i++){ int j = i+len-1; if(j>n) break; if(str[i]=='['&&str[j]==']'||str[i]=='('&&str[j]==')') dp[i][j]=dp[i+1][j-1]+2; for(int k = i;k

 

转载地址:http://vifmz.baihongyu.com/

你可能感兴趣的文章
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_不带分页处理_01_QueryDatabaseTable获取数据_原0036---大数据之Nifi工作笔记0064
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从Oracle11G同步数据到Mysql_亲测可用_解决数据重复_数据跟源表不一致的问题---大数据之Nifi工作笔记0065
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI分页获取Mysql数据_导入到Hbase中_并可通过phoenix客户端查询_含金量很高的一篇_搞了好久_实际操作05---大数据之Nifi工作笔记0045
查看>>
NIFI分页获取Postgresql数据到Hbase中_实际操作---大数据之Nifi工作笔记0049
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
NIFI同步MySql数据源数据_到原始库hbase_同时对数据进行实时分析处理_同步到清洗库_实际操作06---大数据之Nifi工作笔记0046
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
【Flink】Flink 1.9 版本 web UI 突然没有日志
查看>>
NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_处理器介绍_处理过程说明---大数据之Nifi工作笔记0019
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_实际操作---大数据之Nifi工作笔记0020
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_实际操作_02---大数据之Nifi工作笔记0032
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_操作方法说明_01_EvaluteJsonPath处理器---大数据之Nifi工作笔记0031
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>