ural Brackets Sequence (dp)

http://acm.timus.ru/problem.aspx?space=1&num=1183

很经典的问题吧,看的黑书上的讲解。

设dp[i][j]表示i到j括号合法需要的最少括号数。

共有四种情况:

s[i]s[j]配对,dp[i][j] = min( dp[i][j] ,  dp[i-1][j+1] );

s[i] = ‘(‘或‘[‘ dp[i][j] = min( dp[i][j] , dp[i+1][j]+1 );

s[j] = ‘)‘或‘]‘ dp[i][j] = min( dp[i][j] , dp[i][j-1]+1 );

dp[i][j] = min( dp[i][k]+dp[k+1][j] )(i <= k < j-1)

同时拿一个数组记录到达这种状态是哪种方式,然后dfs输出。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 50010;

int dp[110][110];
int way[110][110];
char s[110];
int len;

void dfs(int l, int r)
{
	if(l > r) //少写一句,1wa
		return;
	if(l == r)
	{
		if(s[l] == '(' || s[l] == ')')
			printf("()");
		else if(s[l] == ']' || s[l] == '[')
			printf("[]");
		return;
	}

	if(way[l][r] == 0)
	{
		printf("%c",s[l]);
		dfs(l+1,r-1);
		printf("%c",s[r]);
		return;
	}
	else if(way[l][r] == -1)
	{
		printf("%c",s[l]);
		dfs(l+1,r);
		printf("%c",s[l]=='('?')':']');
		return;
	}
	else if(way[l][r] == -2)
	{
		printf("%c",s[r]==')'?'(':'[');
		dfs(l,r-1);
		printf("%c",s[r]);
		return;
	}
	else
	{
		dfs(l,way[l][r]);
		dfs(way[l][r]+1,r);
		return;
	}

}

void solve()
{
	for(int i = 1; i <= len; i++)
	{
		dp[i][i] = 1;
		dp[i][i-1] = 0;
	}
    for(int p = 1; p <= len-1; p++)
    {
        for(int i = 1; i <= len-p; i++)
        {
            int j = i+p;
            dp[i][j] = INF;
            if( (s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']') )
            {
                if(dp[i][j] > dp[i+1][j-1])
                {
                    dp[i][j] = dp[i+1][j-1];
                    way[i][j] = 0;
                }
            }
            if(s[i] == '(' || s[i] == '[')
            {
                if(dp[i][j] > dp[i+1][j]+1)
                {
                    dp[i][j] = dp[i+1][j]+1;
                    way[i][j] = -1;
                }
            }
            if(s[j] == ')' || s[j] == ']')
            {
                if(dp[i][j] > dp[i][j-1]+1)
                {
                    dp[i][j] = dp[i][j-1]+1;
                    way[i][j] = -2;
                }
            }
            for(int k = i; k <= j-1; k++)
            {
                if(dp[i][j] > dp[i][k]+dp[k+1][j])
                {
                    dp[i][j] = dp[i][k] + dp[k+1][j];
                    way[i][j] = k;
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%s",s+1))
    {
        len = strlen(s+1);
		solve();
		dfs(1,len);
		printf("\n");
    }
    return 0;
}
时间: 2025-01-19 16:02:07

ural Brackets Sequence (dp)的相关文章

poj 1141 Brackets Sequence dp

 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 regu

URAL 1183 Brackets Sequence DP 路径输出

题意:长度小于100的字符串s只由四种字符"()[]"组成,求以该串为子串的最短的合法串.合法串递归定义为: (1)空串合法 (2)如果S合法,则(S).[S]合法 (3)如果A.B合法,则AB合法 思路: 设dp[i][j]为s(i,j)变为合法串后,合法串的长度或需要添加的字符的个数,状态转移: (1)如果s[i]和s[j]匹配,dp[i,j]=dp[i+1,j-1]. (2)如果不匹配,划分s(i,j)为s(i,k)和s(k+1,j),划分后dp[i,j]=dp[i,k]+dp[

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

poj 1141 Brackets Sequence (区间DP)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25893   Accepted: 7295   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 re

POJ 题目1141 Brackets Sequence(区间DP记录路径)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 7885   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 re

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

区间DP [POJ 1141] Brackets Sequence

Brackets Sequence 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, th

POJ 1141 Brackets Sequence (区间DP)

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

ZOJ1463:Brackets Sequence(间隙DP)

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. F