POJ 1141

Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22168   Accepted: 6232   Special Judge

Description

Let us define a regular brackets sequence in the following way: 

1. Empty sequence is a regular sequence. 
2. If S is a regular sequence, then (S) and [S] are both regular sequences. 
3. If A and B are regular sequences, then AB is a regular sequence. 

For example, all of the following sequences of characters are regular brackets sequences: 

(), [], (()), ([]), ()[], ()[()] 

And all of the following character sequences are not: 

(, [, ), )(, ([)], ([(] 

Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.

Input

The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.

Output

Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

Source

Northeastern Europe 2001
 设dp[i,j]为从位置i到位置j需要加入字符的最小次数,有dp[i,j]=min(dp[i,k]+dp[k+1,j]),其中i<=k<j。特别的当s[i]='[' s[j]=']'或者s[i]='(' s[j]=')'时,dp[i,j]=dp[i+1,j-1]。初始条件为dp[i,i]=1,其中0<=i<len。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>


using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))

#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
int dp[MAXN][MAXN];
int path[MAXN][MAXN];
char s[MAXN];
int n;
void print(int l , int r)
{
    if(l > r)return;
    if(l == r)
    {
        if(s[l] == '('||s[l] == ')')cout << "()";
        else cout << "[]";
    }
    else if(path[l][r] == -1)
    {
        cout << s[l];
        print(l + 1 , r - 1);
        cout << s[r];
    }

    else
    {
        print(l , path[l][r]);
        print(path[l][r] + 1 , r);
    }
}
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    while(gets(s)!=NULL)
    {
        n = strlen(s);
        clr(dp , 0);
        clr(path , 0);
        FORR(i , 0 , n){dp[i][i] = 1;dp[i + 1][i] = 0;}
        FOR(k , 1 , n)FOR(i , 0 , n - k)
        {
            int j = i + k;
            dp[i][j] = INF;
            path[i][j] = -1;
            if((s[i] == '('&&s[j] == ')')||(s[i] == '['&&s[j] == ']'))
            {
                    dp[i][j] = min(dp[i][j] , dp[i + 1][j - 1]);
            }
            FOR(l , i , j)if(dp[i][l] + dp[l + 1][j] < dp[i][j])
            {
                dp[i][j] = dp[i][l] + dp[l + 1][j];
                path[i][j] = l;
            }
        }
        print(0 , n - 1);
        cout << endl;
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)