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 a1a2an, 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,dp[i][j]存的是i~j区间内匹配的个数
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int check(char a,char b)
{
if(a=='(' && b==')')
return 1;
if(a=='[' && b==']')
return 1;
return 0;
} int main()
{
char str[105];
int dp[105][105],i,j,k,len;
while(~scanf("%s",str))
{
if(!strcmp(str,"end"))
break;
len = strlen(str);
for(i = 0; i<len; i++)
{
dp[i][i] = 0;
if(check(str[i],str[i+1]))
dp[i][i+1] = 2;
else
dp[i][i+1] = 0;
}
for(k = 3; k<=len; k++)
{
for(i = 0; i+k-1<len; i++)
{
dp[i][i+k-1] = 0;
if(check(str[i],str[i+k-1]))
dp[i][i+k-1] = dp[i+1][i+k-2]+2;
for(j = i; j<i+k-1; j++)
dp[i][i+k-1] = max(dp[i][i+k-1],dp[i][j]+dp[j+1][i+k-1]);
}
}
printf("%d\n",dp[0][len-1]);
} return 0;
}

POJ2955:Brackets(区间DP)的更多相关文章

  1. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  2. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  3. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  4. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  5. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  6. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  7. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  8. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  9. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 转:PHP教程之PHP调用session_start后页面始终加载的问题研究

    今天群里有朋友说他的遇到一个有趣的问题: 一个PHP页面的执行时间比较长(15秒左右),而只要这个页面没有执行完毕,其他的页面访问都是长时间加载状态,只有那个页面执行完毕了,剩下的页面才能打开. 这是 ...

  2. Children of the Candy Corn (bfs+dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8120   Accepted: 3547 Description The c ...

  3. Pascal's Triangle II —LeetCode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  4. Android新浪微博客户端(五)——主界面的TabHost和WeiboUtil

    原文出自:方杰|http://fangjie.info/?p=183转载请注明出处 最终效果演示:http://fangjie.info/?page_id=54 该项目代码已经放到github:htt ...

  5. oracle锁表解决方法

    SQL> select session_id from v$locked_object; SESSION_ID----------142 SQL> SELECT sid, serial#, ...

  6. python学习笔记(集合的使用)

    集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...

  7. JS验证手机号码

    对于表单的验证是添加信息的时候必不可少的. 下面是基于EasyUI-validatebox拓展的对手机.座机号的验证方法. PhoneAndMobile: { validator: function ...

  8. HDU1863 畅通project 【最小生成树Prim】

    畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. android 监听短信数据库,制作短信控制工具,控制别人的手机!!(一)

    序言:本程序示例本着简洁易懂的目的,只做了简单的功能实现,需要用户启动应用,收到短信才有效果.作者将会在后面的(二)篇中加入服务后台运行.自动启动功能,实现一个真正的短信控制工具.本文的目的很简单,让 ...

  10. Cocos2d-x 3.x 头像选择,本地相册图片+图片编辑(Android、IOS双平台)

    大连游戏产业不是很发达,最后,选择一个应用程序外包公司.积累的工作和学习过程中的一点业余生活微信体验,我想分享的游戏小朋友的爱. 在应用开发过程中会经常实用户上传头像的功能,在网上找了N多资料发现没有 ...