Brackets

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 ≤ nai1ai2 … 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[i][j]表示从i到j的最优的稳定态数。最优子结构。
动态转移方程:
  1. 外围:(相隔较远的两者可以构成稳态)dp[i][j]=d[i+1][j-1]+1;
  2. 内部: d[i][j]=max(d[i][j],d[i][k]+d[k+1][j]);(i<=k<j 正好可以相连)

初始化:如果相邻的两个可以构成稳态,即dp[i][j]=1;特殊的dp[i][i]=0;

#include <stdio.h>
#include <string.h>
int f[110][110];
char s[110];
int max(int a,int b)
{
    return a>b?a:b;
}
int mach(int a,int b)
{
    if((s[a]=='['&&s[b]==']')||(s[a]=='('&&s[b]==')'))
        return 1;
    return 0;
}
int main()
{
    int i,j,k,len,g;
    while(scanf("%s",s)&&s[0]!='e')
    {
        memset(f,0,sizeof(f));
        len=strlen(s);
        for(i=0;i<len;i++)
        {
            f[i][i]=0;
            if(mach(i,i+1))f[i][i+1]=1;
        }
        for(k=1;k<len;k++)//从0到任意点的最优值。
              for(i=0;i<len-k;i++)//开始更新
              {
                  j=i+k;
                  if(mach(i,j))f[i][j]=f[i+1][j-1]+1;
                  for(g=0;g<k;g++)
                     f[i][j]=max(f[i][j],f[i][i+g]+f[i+g+1][j]);
              }
        printf("%d\n",f[0][len-1]*2);
    }
    return 0;
}
View Code

还有第二种转移方程:你这么想,在现有的不稳定态中,我们需要添加几个字符才能达到稳态呢?所以我们可以查找不稳定的有多少,再减去这个不稳定态就好了。

动态量:  dp[i][j]表示从i到j中有不稳态的最优解。

动态转移:

  1. 外围:(相隔较远的两者可以构成稳态)dp[i][j]=d[i+1][j-1];
  2. 内部: d[i][j]=min(d[i][j],d[i][k]+d[k+1][j]);(i<=k<j 正好可以相连)

 初始化:如果相邻不是稳态d[i][j]=无穷;特殊的dp[i][i]=1;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
char s[205];
int dp[205][205];

int main()
{
    while(scanf("%s", s) != EOF && strcmp(s, "end") != 0)
    {
        int len = strlen(s);
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < len; i++)
            dp[i][i] = 1;
        for(int l = 1; l < len; l++)
        {
            for(int i = 0; i < len - l; i++)
            {
                int j = i + l;
                dp[i][j] = INF;
                if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']'))
                    dp[i][j] = dp[i + 1][j - 1];
                for(int k = i; k < j; k++)
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
            }
        }
        printf("%d\n", len - dp[0][len - 1]);
    }
}
View Code

 

posted @ 2017-08-17 14:50  Hunter丶安  阅读(535)  评论(0编辑  收藏  举报