poj 2955 Brackets(区间dp)

来源:互联网 发布:时时彩平台源码下载 编辑:程序博客网 时间:2024/05/16 15:11

北京大学暑期课:《ACM/ICPC竞赛训练》面向全球招生

Language:
Brackets
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3907 Accepted: 2053

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 ≤ 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

66406


给出只有()[]的字符串  求字符串中做多的()[]匹配的个数


dp[i][j]表示区间i到j中字符匹配最多的个数  一开始假设字符不匹配 dp[i][j]=dp[i+1][j] 

如果在区间i+1到j之间有字符与i匹配 dp[i][j]=dp[i+1][k-1]+dp[k][j]+2

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}char ch[110];int dp[110][110];int main(){//    fread;    while(scanf("%s",ch)!=EOF)    {        if(strcmp(ch,"end")==0) break;        int len=strlen(ch);        MEM(dp,0);        for(int i=len-2;i>=0;i--)        {            for(int j=i+1;j<len;j++)            {                dp[i][j]=dp[i+1][j];                for(int k=i+1;k<=j;k++)                {                    if(ch[i]=='('&&ch[k]==')'||ch[i]=='['&&ch[k]==']')                    {                        dp[i][j]=max(dp[i][j],dp[i+1][k-1]+dp[k][j]+2);                    }                }            }        }        printf("%d\n",dp[0][len-1]);    }    return 0;}





0 0