Brackets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5424   Accepted: 2909

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 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
 
题意:有一串括号,() 或者 [ ]这样算作匹配,如果s1匹配了,那么(s1) [s1]也算匹配,其他都是不合法的。
输入一串括号,问你匹配的长度是多少。
 
思路:
区间dp。dp[i]j]表示以i开始的长度为j的匹配的个数。转移的时候还需要考虑 j 到 j+i 之间的这一段,设其最大值x,
如果s[j] == s[j+i] ,x += 1.然后就是状态转移的方程 dp[j][i] = max(x,dp[j][k] + dp[k+j][i-k] | k表示<=i的长度);
因为只进行状态转移的话,无法考虑匹配的情况,所以我先处理匹配时候的值。
 
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
char s[MAXN];
int ok(char k1,char k2)
{
if(k1 == '(' && k2 == ')')
return ;
if(k1 == '[' && k2 == ']')
return ;
return ;
}
int main()
{
while(~scanf("%s",s+)){
if(s[] == 'e')break;
int len = strlen(s+);
int ans = ;
memset(dp,,sizeof(dp));
for(int i = ; i <= len; i++){
for(int j = ; j <= len - i + ; j++){
if(i == && ok(s[j],s[j+i-])){
dp[j][i] = ;
}
else if(i > ){
int p = ;
for(int k = ; k <= i - ; k++){
p = max(p,dp[j+][k]+dp[j+k+][i-k-]);
//cout<<i<<endl;
//cout<<dp[j+1][k]<<' '<<j+1<<' '<<k<<' '<<endl;
//cout<<dp[j+k+1][i-k-1]<<' '<<j+k+1<<' '<<i-k-1<<' '<<endl;
}
if(ok(s[j],s[j+i-])){
p++;
}
dp[j][i] = max(p,dp[j][i]);
for(int k = ; k <= i; k++){
dp[j][i] = max(dp[j][i],dp[j][k]+dp[j+k][i-k]);
}
}
}
}
printf("%d\n",dp[][len] * );
}
return ;
}

poj2955括号匹配 区间DP的更多相关文章

  1. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  2. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  3. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  4. [poj2955/nyoj15]括号匹配(区间dp)

    解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少 ...

  5. UVA 1626 Brackets sequence(括号匹配 + 区间DP)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...

  6. HDU4632 Poj2955 括号匹配 整数划分 P1880 [NOI1995]石子合并 区间DP总结

    题意:给定一个字符串 输出回文子序列的个数    一个字符也算一个回文 很明显的区间dp  就是要往区间小的压缩! #include<bits/stdc++.h> using namesp ...

  7. POJ2955:Brackets(区间DP)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  8. TZOJ 3295 括号序列(区间DP)

    描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...

  9. POJ2955 Brackets (区间DP)

    很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...

随机推荐

  1. Discuz论坛搬家手记(X3.2版本)

    Discuz论坛搬家手记(X3.2版本) 客户在虚拟主机上 运行着一个DISCUZ论坛, 主机商限制太多,连MP3都不让放,客户准备是在上面放一个FLASH斗地主游戏的(多人联机版), 加上MYSQL ...

  2. div根据内容改变大小并且左右居中

    div{ display:inline-block; width:auto; } 这个div的父元素text-align:center;

  3. [No000065]python 获取当前时间

    要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间.用下面的方式来取得当前时间的时间戳: import time print(time.time()) 输出的结果是 ...

  4. Canon

    .打开压缩包发现2个文件,1个是加密的zip.zip,另一个是music.mp3.猜测zip.zip的解压密码隐藏在music.mp3里 .用MP3Stego解密music.mp3,密码就是标题的Ca ...

  5. linux下DHCP服务原理总结

    DHCP(全称Dynamic host configuration protocol):动态主机配置协议DHCP工作在OSI的应用层,可以帮助计算机从指定的DHCP服务器获取配置信息的协议.(主要包括 ...

  6. jquery两个滚动条样式

    jquery两个滚动条样式 点击下载

  7. python子网拆分IP段

    # author: 阿辉枫情 import re import struct import socket def IPSplitBlocks(minip, maxip): result = [] Ju ...

  8. Could not load file or assembly 'System.Data.SQLite' or one of its dependencies

    试图加载格式不正确的程 异常类型 异常消息Could not load file or assembly 'System.Data.SQLite' or one of its dependencies ...

  9. Linux wait函数详解

    wait和waitpid出现的原因 SIGCHLD --当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止) --子进程退出时,内核将 ...

  10. EMV内核使用中的常见问题

    EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...