简单区间DP (有空串... ...)

Brackets sequence
Time Limit: 4500MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 a1a2...an is called a subsequence of the string b1b2...bm, if there exist such indices 1 ≤ i1 < i2 < ... < in ≤ m, that aj=bij for all 1 ≤ j ≤ n.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

1([(]

Sample Output

()[()]

Source

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 9. Dynamic Programming :: Examples

Submit Status

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月11日 星期三 16时15分08秒
File Name     :ZOJ1463.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;const int maxn=300;
const int INF=0x3f3f3f3f;char str[maxn];
int n;
int dp[maxn][maxn];bool match(int a,int b)
{if((str[a]=='('&&str[b]==')')||(str[a]=='['&&str[b]==']'))return true;return false;
}void PRINT(int l,int r)
{if(l==r){if(str[l]=='('||str[l]==')'){putchar('('); putchar(')');}if(str[l]=='['||str[l]==']'){putchar('['); putchar(']');}return ;}else if(l>r) return ;int pos=-1;int temp=INF;if(match(l,r)) temp=dp[l+1][r-1];for(int i=l;i+1<=r;i++)if(dp[l][i]+dp[i+1][r]<temp){pos=i; temp=dp[l][i]+dp[i+1][r];}if(pos==-1){putchar(str[l]); PRINT(l+1,r-1); putchar(str[r]); }else {PRINT(l,pos); PRINT(pos+1,r);}
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int T_T,flag=0;scanf("%d",&T_T);getchar();while(T_T--){gets(str);memset(str,0,sizeof(str));gets(str);n=strlen(str);if(n==0) { if(flag++) putchar(10);putchar(10); continue; }/// DPmemset(dp,63,sizeof(dp));for(int i=0;i<n;i++) dp[i][i]=1;for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(i>j) dp[i][j]=0;for(int len=2;len<=n;len++){for(int i=0;i+len-1<n;i++){/// i...jint j=i+len-1;if(match(i,j)) dp[i][j]=min(dp[i][j],dp[i+1][j-1]);for(int k=i;k+1<=j;k++)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);}}if(flag++) putchar(10);PRINT(0,n-1);putchar(10);}return 0;
}

UVA1626 / ZOJ1463 Brackets sequence 区间DP相关推荐

  1. 紫书动规 例题9-10 UVA - 1626 Brackets sequence 区间dp

    题目链接: https://vjudge.net/problem/UVA-1626 题意: 题解: dp[i][j]:= i~j需要最少的括号 区间dp: dp[i][j] = min(dp[i][j ...

  2. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. Brackets (区间DP)

    个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对 对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越 ...

  4. POJ - 2955 Brackets (区间DP)

    题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...

  5. UVA1626 括号序列 Brackets sequence(区间DP匹配括号,输出匹配方案)

    整理的算法模板合集: ACM模板 UVA1626 Brackets sequence 我们将正规括号序列定义如下: 空序列是正规括号序列. 如果 SSS 是一个正规括号序列,那么 (S) 和 [S] ...

  6. (区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955

    http://poj.org/problem?id=2955 Description We give the following inductive definition of a "reg ...

  7. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  8. hdu 5273 Dylans loves sequence 逆序数 区间dp

    点击打开链接 题意:给n个数,q次询问,(L,R)区间内的逆序数. 思路: 区间dp 代码一: 1 #include <bits/stdc++.h> 2 using namespace s ...

  9. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

最新文章

  1. POJ 3174 暴力枚举
  2. Vue2.0增删改查案例(Vue+Less+LocalStorage)
  3. 计算平面坐标某点(x,y)与原点(0,0)的角度
  4. 网易容器云平台的微服务化实践
  5. python export_django-import-export插件使用教程
  6. ibmt42装Android,IBM T42系统升级初步体会,爽!
  7. 正点原子操作过程中芯片总是出错
  8. ZK框架笔记3、窗体组件
  9. iOS.UIKit.07.UIAlertView_UIActionSheet
  10. Ubuntu下升级安装gcc-7.5.0教程
  11. (转)别只盯着比特币!“野蛮生长”的ICO江湖:2年30倍只是寻常
  12. 线性规划问题(excel和python)
  13. 教务管理系统的设计与实践
  14. 设计模式--C++学习(4)
  15. 大数据热点案例(含图)
  16. 计算机音乐雅俗共赏,“雅俗共赏”与音乐教育
  17. 【总览】程序员前端、后端资源合集
  18. ProE5.0塑胶产品结构设计实例知识视频教程
  19. oppoa9处理器怎么样_OPPOA9处理器是骁龙几?
  20. Altium Designer生成Gerber文件的设置(嘉立创建议 )

热门文章

  1. 计算机专业可以考天文学研究生吗,天文学专业强势高校来袭 看似冷门考生却挤破头都想去...
  2. 51nod1326 遥远的旅途(spfa+dp)
  3. 【深度学习】Mask-RCNN 计算机视觉实例分割模型介绍 Mask分支
  4. 华为笔记本没有网线口_matebook 14有网线接口吗
  5. #Linux基础(三)
  6. excel字符串和单元格拼接_excel拼接函数_excel使用公式进行文本拼接的方法
  7. Unity 2D游戏制作
  8. RK3399 Android7.1 充电芯片bq25723发生ACOV后引起系统黑屏死机
  9. Could not delete path ‘D:\AndroidStudioProjects\LargeScreen\app\build\generated\source\r\debug\andro
  10. 优麒麟 20.04 LTS Pro 发布 - 以初心,铸匠心