标签: ACM


题目:

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

解题思路:

两种满足的条件

第一种如果两括号匹配则等于中间的匹配+2

第二种区间的匹配等于区间中的相邻区间匹配之和

将2个匹配到最大个数匹配遍历一遍最后就是答案

还是直接看AC代码容易理解些吧.......

#include <iostream>
#include <string.h>
using namespace std;
int dp[105][105]; //表示从i到j字符串的最大匹配
bool match(char a ,char b)
{
return ((a=='('&&b==')')||(a=='['&&b==']'));
}
int max(int a,int b)
{
return (a>b?a:b);
}
int main()
{
string s;
int len,i,j,k,t;
while(cin>>s)
{
memset(dp,0,sizeof(dp));
if(s=="end")
break;
len=s.size();
for(i=1;i<len;i++) //匹配i+1长度的字符串
for(j=0,k=i;k<len;j++,k++)
{
if(match(s[j],s[k])) //寻找最大包含匹配
dp[j][k]=dp[j+1][k-1]+2; //如果j到k匹配,则等于两者中间的匹配度加2
for(t=j;t<k;t++) //寻找最大相邻匹配
dp[j][k]=max(dp[j][k],dp[j][t]+dp[t+1][k]);
}
cout<<dp[0][len-1]<<endl;
}
return 0;
}

状态压缩- Brackets的更多相关文章

  1. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  2. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  3. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  4. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  7. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  8. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  9. poj3254 状态压缩dp

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法.     分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...

随机推荐

  1. Android基础知识05—活动的生命周期

    ------ 活动的生命周期 ------ Android是使用任务Task来管理活动的,一个任务就是一组存放在栈里的活动的集合.每当启动一个活动 ,他就会在返回栈中入栈,并处于栈顶位置.而每当我们按 ...

  2. 如何学习LoadRunner性能测试?

    最近组内同事针对性能测试LR的脚本部分做了介绍,是个不错的分享.会后反思自己也有很长一段时间没做性能测试了,根据以往的经验,有必要做些整理和补充,本文主要介绍一些Loadrunner性能测试的学习方法 ...

  3. css雪碧图(css splite)

    将很多小的背景图片放在一起,可以减少http请求. 这些图片通常是一类的. 所以使用雪碧图. 雪碧图即为: 测试一下减少了多长时间 0 = 0

  4. iOS之 重绘机制

    最近在看Core Animation , 今天来谈谈CALayer 和 UIView 中的重绘的一些认识: 我们都知道UIView里面有个成员layer,利用这个这个layer我们可以设置一些圆角,阴 ...

  5. 在windows下,将mysql离线数据文件导入本地mysql数据库

    1. 查看mysql路径 SELECT @@basedir AS basePath FROM DUAL 其实mysql5.6 的数据文件在 C:\ProgramData\MySQL\MySQL Ser ...

  6. fiddler学习资源

    小坦克   fiddler教程:http://www.cnblogs.com/TankXiao/archive/2012/04/25/2349049.htmlps:另外博主其他测试文章也值得一看 涂根 ...

  7. C++计时器:毫秒级和微秒级

    1.毫秒级 使用GetTickCount()获取系统启动所经过的毫秒数 #include<iostream> using namespace std; int main(){ DWORD ...

  8. java语言将任意一个十进制数数字转换为二进制形式,并输出转换后的结果

    package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Test { /* * 将任 ...

  9. robotframework2.8.7日志输出List乱码

    今天在完善robotframework的数据分离的时候,需要读取list中的中文信息,在日志输出中,英文显示是正常的:但是中文就出现所谓的乱码情况,如截图所示 对于list中的信息,日志显示乱码:查找 ...

  10. SeaJS之shim插件:解决非cmd规范的插件与sea的区别

    SeaJS 中的模块默认都遵守 CMD 规范,但现实中已存在大量普通 JavaScript 类库,比如 jQuery.Underscore 等.使用 shim 插件,可以将这些普通 JS 文件转换成 ...