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. 解决xp下无法通过windows installer服务安装此安装程序包。您必须安装带有更新版本Wi

    今天装 tortoisegit 的时候发现安装包不能使用.报错无法通过windows installer服务安装此安装程序包.您必须安装带有更新版本Windows Installer服务的Window ...

  2. SQLite for xamarin

    原文地址:http://www.codeproject.com/Articles/1097179/SQLite-with-Xamarin-Forms-Step-by-Step-guide SQLite ...

  3. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  4. 【2013长沙区域赛】部分题解 hdu4791—4801

    1001: 签到题,二分一下即可 代码: #include <set> #include <map> #include <cmath> #include <c ...

  5. [Java] 模板引擎 Velocity 随笔

    Velocity 是一个基于 Java 的模板引擎. 本博文演示 Velocity 的 HelloWord 以及分支条件. HelloWord.vm,模板文件. templateDemo.java, ...

  6. [Java Concurrent] 多线程合作 producer-consumers / queue 的简单案例

    在多线程环境下,通过 BlockingQueue,实现生产者-消费者场景. Toast 被生产和消费的对象. ToastQueue 继承了 LinkedblockingQueue ,用于中间存储 To ...

  7. arc4random和arc4random_uniform

    Objective-C 中有个arc4random()函数用来生成随机数且不需要种子,但是这个函数生成的随机数范围比较大,需要用取模的算法对随机值进行限制,有点麻烦. 其实Objective-C有个更 ...

  8. OBJ-C

    1.直接赋值 NSString *name = @"Starain"; 2.用已经存在的字符串进行初始化 NSString *name2 = [NSString stringWit ...

  9. HTTP学习实验8-windows添加telnet功能

    Windows 添加telnet功能: 控制面板->(查看方式:小图标)->程序和功能->打开或关闭Windows功能->Telnet客户端 Telnet 设置: 打开cmd, ...

  10. javascript基础笔记学习

    /** * Created by Administrator on 2016/12/26. */ /* var box; alert( typeof box); box是Undefined类型,值是u ...