URAL1183——DFS+回溯—— 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, 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 a 12...a n is called a subsequence of the string b 12...b m, if there exist such indices 1 ≤ i 1 < i 2 < ... < i n ≤ m, that a j=b ij for all 1 ≤ j ≤ n.

Input

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

Output

Write 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

input output
([(]
()[()]

大意:匹配最少的括号并输出

定义dp[i][j] 表示从i到j最少所需要匹配的括号 ,定义pos[i][j] 表示从i到j是否能括号匹配

状态转移方程   dp[x][y] = min(dp[x][y],dp[x][i]+dp[i+1][y])

杰哥代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[110][110];
int pos[110][110];
char s[110];
const int inf = 0x3f3f3f3f;
void dfs(int x,int y)
{
    if(dp[x][y] != -1) return ;
    if(x > y) {
        dp[x][y] = 0;
        return ;
    }
    if(x == y){
        dp[x][y] = 1;
        return ;
    }
     dp[x][y] = inf;
    if((s[x] == ‘(‘ && s[y] == ‘)‘)||(s[x] == ‘[‘ && s[y] == ‘]‘)){
        pos[x][y] = -1;
        dfs(x+1,y-1);
        dp[x][y] = dp[x+1][y-1];
    }
    for(int i = x; i < y; i++){
        dfs(x,i);
        dfs(i+1,y);
        if(dp[x][i] + dp[i+1][y] < dp[x][y]){
            dp[x][y] = dp[x][i] + dp[i+1][y];
            pos[x][y] = i;
        }
    }
}
void print(int x,int y)
{
    if(x > y)
    return ;
    if(x == y){
        if(s[x] == ‘(‘ || s[x] == ‘)‘)
            printf("()");
        else
            printf("[]");
        return ;
    }
    if(pos[x][y] == -1){
        printf("%c",s[x]);
        print(x+1,y-1);
        printf("%c",s[y]);
    }

    else {
        print(x,pos[x][y]);
        print(pos[x][y]+1,y);
    }
}
int main()
{
    scanf("%s",s+1);
    int n = strlen(s+1);
    memset(dp,-1,sizeof(dp));
    dfs(1,n);
    print(1,n);
    printf("\n");
   return 0;
}

  

时间: 2024-12-28 21:59:35

URAL1183——DFS+回溯—— Brackets Sequence的相关文章

记忆化搜索+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

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

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

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

UVa 1626 Brackets sequence (动态规划)

题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配,枚举中间断点k,如果str[i]==str[k]则dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k+1][j])表示不需要加入新的括号,也可以插入新的括号则dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]+1). 如果区间最左边字符

POJ1141 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, then AB is a regular

CodeForces 550B Preparing Olympiad(DFS回溯)

[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每个题目有相应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x,求选择方案数. [解题思路]: DFS+回溯. 先发一发比较拙的代码: #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int num[N],mum[N]; int n,m,q,t,l,r; int top,ans,

HDU4499 Cannon DFS 回溯的应用

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过 #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #

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