博客
关于我
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/

你可能感兴趣的文章
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>