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. Solr4.8.0源码分析(10)之Lucene的索引文件(3)

    Solr4.8.0源码分析(10)之Lucene的索引文件(3) 1. .si文件 .si文件存储了段的元数据,主要涉及SegmentInfoFormat.java和Segmentinfo.java这 ...

  2. POJ1125 Stockbroker Grapevine(最短路)

    题目链接. 分析: 手感不错,1A. 直接穷举的起点, 求出不同起点到其它点最短路中最长的一条的最小值(好绕). #include <iostream> #include <cstd ...

  3. HDU4536 XCOM Enemy Unknown(dfs)

    题目链接. 分析: 用dfs枚举每一波攻击的三个国家. 很暴力,但没想到0ms. #include <iostream> #include <cstdio> #include ...

  4. POJ2253 Frogger(最短路)

    题目链接. 题意: 从0号点,到1号点,找一条能通过的路,使得这条路中的最大的边,比其它所有可能的路中的边都小. 分析: 这题就是按着dijkstra写,写着写着觉得像是prim了. 其中d[n]表示 ...

  5. -_-#【Better Code】字符串匹配

    提高 web 应用性能之 JavaScript 性能调优

  6. freemarker基本知识总结

    1.取出内容 ${} 例如,${document.fileName} 2. <ul> <#list cms.documents("channel=XXXX") a ...

  7. ASPNET登陆总结

    昨天晚上看了视频,今天早上起来就凭着记忆与视频里的代码试着做了一个登陆,基本功能是实现了. 0x0:首先,第一步是做一个界面....直接扒别人做好的页面.....各种改改路径啥的,用浏览器打开,恩,发 ...

  8. Shell遍历文件的每一行

    由于使用for来读入文件里的行时,会自动把空格和换行符作为一样分隔符,因为当行里有空格的时候,输出的结果会很乱,所以…… cat input.txt |while read line> do&g ...

  9. Selenium webdriver 元素操作

    本来这些东西网上一搜一大堆,但是本着收集的精神,整理一份放着吧!哈!哈!哈! 1. 输入框(text field or textarea) WebElement element = driver.fi ...

  10. 如何给DropDownList控件设置样式(ASP.NET MVC)

    前话: 应学校领导要求,要给后台管理系统添加一个搜索功能,提供可选择选项.我选择使用DropDownList去实现,熟悉.net控件的都知道,DropDownList的样子非常丑,不论是边框长宽还是里 ...