题意:

给你一个括号序列(有中小括号),求出以给定序列为子序列的最小合法括号序列。

分析:

非常经典,以前做过相似一道题,用区间dp,但怎么把这个序列求出来没想出来。

dp[i][j]表示区间i-j是序列合法要增加括号的最小数量,并pos[i][j]表示i-j在哪个位置断开最小,最后通过递归位置打印出答案。

此题序列可能是空串。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[][],pos[][],len;
char s[];
void solve(){
memset(dp,,sizeof(dp));
for(int i=;i<len;++i)
dp[i][i]=;
for(int l=;l<len;++l)
for(int i=;i+l<len;++i){
int j=i+l;
int minv=INF;
for(int k=i;k<j;++k){
if(dp[i][k]+dp[k+][j]<minv)
{
minv=dp[i][k]+dp[k+][j];
pos[i][j]=k;
}
}
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']'){
if(dp[i+][j-]<minv){
minv=dp[i+][j-];
pos[i][j]=-;
}
}
dp[i][j]=minv;
}
}
void dfs(int l,int r){
if(l>r)return;
if(l==r){
if(s[l]=='('||s[l]==')')
printf("()");
else printf("[]");
}
else{
if(pos[l][r]>=){
dfs(l,pos[l][r]);
dfs(pos[l][r]+,r);
}
else{
if(s[l]=='('){
printf("(");
dfs(l+,r-);
printf(")");
}
else{
printf("[");
dfs(l+,r-);
printf("]");
}
}
}
}
int main()
{
int ca=,t;
scanf("%d",&t);
getchar();
while(t--){
gets(s);
gets(s);
memset(pos,-,sizeof(pos));
len=strlen(s);
if(len==)
{
printf("\n");
if(t)printf("\n");
continue;
}
solve();
dfs(,len-);
printf("\n");
if(t)printf("\n");
}
return ;
}

Brackets sequence的更多相关文章

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

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  2. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  3. POJ1141 Brackets Sequence

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  4. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  5. ZOJ1463:Brackets Sequence(间隙DP)

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  6. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  7. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  8. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

  9. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  10. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

随机推荐

  1. SDK 与MFC

    SDK 就是Software Development Kit 软件开发包MFC 就是Microsoft Foundation Classes 微软函数类库.是以C++类的形式封装了Windows的AP ...

  2. QC、IQC、IPQC、FQC、OQC

    品质政策为:全面品管.贯彻制度.提供客户需求的品质:全员参与.及时处理.以达成零缺点的目标. 品质三不政策为:不接受不良品.不制造不良品.不流出不良品. QC即英文QUALITY CONTROL的简称 ...

  3. C# 任意类型数据转JSON格式(转)

    HOT SUMMER 每天都是不一样,积极的去感受生活 C# 任意类型数据转JSON格式 /// <summary> /// List转成json /// </summary> ...

  4. JQuery与DOM中的区别

    一.Query与DOM的区别 1.页面加载: DOM:window.onload=function(){}; JQuery:$(function(){ }); 2.获取对象:JQuery中有“#” D ...

  5. JVM最多可创建多少线程

    JVM可支持的最大线程数 JVM最大线程数 (2012-07-04 23:20:15) 转载▼ 标签: jvm 最大线程数 it 分类: java分布式总结 摘自:http://sesame.itey ...

  6. 使用Retrofit时出现 java.lang.IllegalArgumentException: URL query string "t={type}&p={page}&size={count}" must not have replace block. For dynamic query parameters use @Query.异常原因

    /** * Created by leo on 16/4/30. */ public interface GanchaiService { @GET("digest?t={type}& ...

  7. Java API —— 多线程

    1.多线程概述     1)进程:         正在运行的程序,是系统进行资源分配和调用的独立单位.         每一个进程都有它自己的内存空间和系统资源.     2)线程:         ...

  8. ios越狱开发第一次尝试记录

    1.THEOS的makefile文件中的THEOS_DEVICE_IP要写在第一行 2.如果make package install报错 dpkg status database is locked ...

  9. System,Integer,Calendar,Random和容器

    System 1)arraycopy int[] a = {1.2.3.4}; int[] b = new int[5]; System.arraycopy(a,1,b,3,2); //把数组a中从下 ...

  10. Bitset小结 (POJ2443 & HDU4920)

    学了下bitset用法,从网上找的一些bitset用法,并从中调出一些常用的用法. 构造函数bitset<n> b; b有n位,每位都为0.参数n可以为一个表达式.如bitset<5 ...