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

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
char s[maxn];
int dp[maxn][maxn],choose[maxn][maxn];
void printstr(int i,int j)
{
if(i>j)
return ;
if(i==j)
{
if(s[i]=='('||s[i]==')') printf("()");
else printf("[]");
return;
}
if(choose[i][j]==-)
{
printf("%c",s[i]);
printstr(i+,j-);
printf("%c",s[j]);
}
else
{
printstr(i,choose[i][j]);
printstr(choose[i][j]+,j);
}
}
int main()
{
int t;
//scanf("%d",&t);
cin>>s; int len =strlen(s);
for(int i=; i<len; i++)
dp[i][i]=,dp[i+][i]=;
for(int p=; p<len; p++)
{
for(int i=,j=i+p; j<len; i++,j++)
{
dp[i][j]=inf;
choose[i][j]=-;
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
dp[i][j]=min(dp[i][j],dp[i+][j-]);
for(int k=i; k<j; k++)
{
if(dp[i][j]>dp[i][k]+dp[k+][j])
{
choose[i][j]=k;
dp[i][j]=dp[i][k]+dp[k+][j];
}
}
}
}
printstr(,len-);
printf("\n");
return ;
}

poj 1141 区间dp+递归打印路径的更多相关文章

  1. DP(递归打印路径) UVA 662 Fast Food

    题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...

  2. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  3. POJ 1141 经典DP 轨迹打印

    又几天没写博客了,大二的生活实在好忙碌啊,开了五门专业课,每周都是实验啊实验啊实验啊....我说要本月刷够60题,但好像完不成了,也就每天1题的样子.如今写动规还是挺有条理的,包括这道需要打印轨迹,其 ...

  4. POJ 1141 区间DP

    给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...

  5. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  6. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  7. poj 3280(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 38 ...

  8. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  9. POJ 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

随机推荐

  1. SAP打印出库单需求

    *&---------------------------------------------------------------------* *& Report  Z_SD_CKD ...

  2. Centos6.5 SVN服务器 搭建及配置

    现有的项目开发中,版本控制机必不可少.合理的使用版本控制可以提高开发效果,在保证项目是最新的同时,也提高了源代码的安全性. 工具/原料 接入Internet的一台Centos6.5Linux计算机 安 ...

  3. Mathematics:Semi-prime H-numbers(POJ 3292)

      Semi-prime H-numbers 题目大意,令4n+1的数叫H数,H数素数x的定义是只能被x=1*h(h是H数),其他都叫合数,特别的,当一个数只能被两个H素数乘积得到时,叫H-semi数 ...

  4. codeforces 519C. A and B and Team Training 解题报告

    题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个  experienced participants  和 m 个 newbie ...

  5. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  6. 20145213《Java程序设计》实验五Java网络编程及安全

    20145213<Java程序设计>实验五Java网络编程及安全 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传输系统. 实验预期 1.客户端与服务器 ...

  7. 把Tomcat做成系统服务自动启动

    用Tomcat的bin目录下的service.bat,cmd,命令:进入到Tomcat的bin目录 service.bat install可以把tomcat做成系统服务;修改下计算机管理里面的服务,找 ...

  8. Jquery选中行实现行中的Checkbox的选中与取消选中

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  9. 解决At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this log

    pom增加:<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</art ...

  10. QRTZ_表注释

    QRTZ_CALENDARS 存储Quartz的Calendar信息QRTZ_CRON_TRIGGERS 存储CronTrigger,包括Cron表达式和时区信息QRTZ_FIRED_TRIGGERS ...