uva 1626 - Brackets sequence【DP】

Keywords: PHP

The main idea of the topic

Portal

Define the following regular parentheses sequence:
- Null sequence is normal parentheses sequence
- If S is a normal bracket sequence, then [S] and (S) are also normal bracket sequences.
- If both A and B are regular parentheses, then AB is also normal parentheses.

Now give a sequence of parentheses, and ask how many characters can be added to it at least to become a regular sequence of parentheses.

thinking

Let dp[i][j] be at least the number of parentheses that need to be added to the string S[i]~S[j].
There are two ways to convert parentheses
1. If S[i] = S[j] in dp[i][j], then dp[i][j] = dp[i+1][j-1]
2. If dp[i][j] has at least two characters, then dp[i][j] = min (dp [i] [k], DP [k + 1] [j] | I < k<= j)

Initial state: dp[i][i] =1

Code

#include<stdio.h>
#include<cstring>
#include<set>
#define maxn 105
#define INF 1<<30
#define min(a,b) (a>b)? b:a
#define max(a,b) (a>b)? a:b
using namespace std;

char S[maxn];
int dp[maxn][maxn];

bool match(int i,int j)
{
    if(S[i] == '(' && S[j] == ')') return true;
    if(S[i] == '[' && S[j] == ']') return true;
    return false;
}

void print(int i,int j)
{
    //printf("\n%d %d\n",i,j);
    if(i>j) return;
    if(i==j)
    {
        if(S[i] == '(' || S[i]==')') printf("()");
        else printf("[]");
    }
    if(match(i,j) && dp[i][j]==dp[i+1][j-1])
    {
        printf("%c",S[i]);
        print(i+1,j-1);
        printf("%c",S[j]);
        return;
    }
    for(int k = i; k<j; k++)
    {
        if(dp[i][j] == (dp[i][k] + dp[k+1][j]))
        {
            print(i,k);
            print(k+1,j);
            return;
        }
    }
}

int main ()
{
    int N;
    scanf("%d",&N);
    getchar();
    //freopen("out.txt","w",stdout);
    while(N--)
    {
        //scanf("%s",S+1);
        getchar();
        for(int i = 1;; i++)
        {
            S[i] = getchar();
            if(S[i] == '\n')
            {
                S[i]='\0';
                break;
            }
        }
        int len = strlen(S+1);

        for(int i = 1; i<=len; i++) dp[i][i] = 1,dp[i][i+1] = 0;

        for(int i = len-1; i>=1; i--)
        {
            for(int j = i+1; j<=len; j++)
            {
                dp[i][j] = INF;
                if(match(i,j)) dp[i][j] = min(dp[i][j],dp[i+1][j-1]);
                int tmp = INF;
                for(int k = i; k < j; k++)
                {
                    tmp = min(tmp,dp[i][k]+dp[k+1][j]);
                }
                dp[i][j] = min(dp[i][j],tmp);
            }
        }
        //printf("%d\n",dp[1][len]);

        print(1,len);
        printf("\n");
        if(N!=0) printf("\n");
    }
    //fclose(stdin);
}

/*
Sample Input
1
([(]
Sample Output
()[()]
*/

Hit

The input of this topic is very problematic and the output is also very problematic.
In fact, when entering debug mode debugging sample, remember to log out the output file after debugging success...

Posted by Dingbats on Thu, 07 Feb 2019 22:51:16 -0800