poj 2955 Brackets (区间dp)||(记忆话搜素)

来源:互联网 发布:hifi音频播放软件 编辑:程序博客网 时间:2024/05/16 15:25

http://poj.org/problem?id=2955

Brackets
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4797 Accepted: 2550

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

66406

区间dp,第i个符号和i之后的所有符号分别进行匹配,当i和第i到len之间的符号进行匹配成功,即第k个符号匹配成功,弱第i个符号和第i+1个符号(即k==i+1)匹配成功的话,dp[i][j] = max (dp[i][j], dp[k+2][j]+2);当k!=i+1时,dp[i][j] = max (dp[i][j], dp[i+1][k-1]+dp[k+1][j]+2);

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;typedef long long LL;#define N 175#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-5#define met(a, b) memset (a, b, sizeof (a))char str[N];int dp[N][N];int main (){    while (scanf ("%s", str), strcmp (str, "end"))    {        met (dp, 0);        int len = strlen (str);        for (int i=len-1; 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 ((str[i] == '(' && str[k] == ')') || (str[i] == '[' && str[k] == ']'))                    {                        if (k == i+1) dp[i][j] = max (dp[i][j], dp[k+1][j]+2);                        else dp[i][j] = max (dp[i][j], dp[i+1][k-1] + dp[k+1][j] + 2);                    }                }            }        }        printf ("%d\n", dp[0][len]);    }    return 0;}

记忆化搜索

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;typedef long long LL;#define N 175#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-5#define met(a, b) memset (a, b, sizeof (a))char str[N];int dp[N][N];int judge (int sa, int en){    if ((str[sa]=='(' && str[en]==')')|| (str[sa]=='[' && str[en]==']')) return 2;    return 0;}int DaTaiyang (int sa, int en){    if (dp[sa][en] != -1) return dp[sa][en];    if (sa >= en) return 0;    if (sa+1 == en) return judge (sa, en);    dp[sa][en] = DaTaiyang (sa+1, en);    for (int i=sa+1; i<=en; i++)        if (judge (sa, i))            dp[sa][en] = max (dp[sa][en], DaTaiyang (sa+1, i-1) + DaTaiyang (i+1, en) + 2);    return dp[sa][en];}int main (){    while (scanf ("%s", str), strcmp (str, "end"))    {        met (dp, -1);        printf ("%d\n", DaTaiyang (0, strlen(str)-1));    }    return 0;}


1 0
原创粉丝点击