题意:

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

分析:

非常经典,以前做过相似一道题,用区间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. hdu 1329 Hanoi Tower Troubles Again!

    找规律的题目an=an-1+(i+i%2)/2*2; ;}

  2. Java Socket文件上传

    客户端: import java.io.FileInputStream; import java.net.Socket; /** * Created by 290248126 on 14-5-11. ...

  3. QAQ高精度模板笔记√

    #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #i ...

  4. STL函数模板(即算法)一览

    查找算法 adjacent_find:找出一个串中第一个不符合次序的地方 find,find_if:找出第一个符合条件的元素 find_first_of:在一个串中寻找第一个与另一个串中任意一个元素相 ...

  5. java登陆验证码与JS无刷新验证

    最近公司的项目的登陆模块由我负责,所以就做了个登陆小功能进行练手,其包括了用jQuery对用户名和密码进行不为null验证,和出于安全性考虑加了一个验证码的校验 别的不说先上代码 controller ...

  6. asp.net如何设置数据库连接池的数量

    http://www.cnblogs.com/wbcms/archive/2008/10/11/1308725.html 可以使用一组名称-值对以链接字符串的形式配置链接池.例如,可以配置池是否有效( ...

  7. Unigui有用的网址

    http://www.cnblogs.com/ChinaEHR/tag/Delphi/

  8. Linux使用者管理(1)---用户账号

    linux很重要的应用就是作为服务器的操作系统.服务器的作用是给多用户提供各种“服务”(可能是读服务器上的文件,或者是利用服务器进行数值计算)那么如果多用户共同拥有一台服务器,就需要对服务器上的用户进 ...

  9. makefile使用

    linux make手册:http://www.gnu.org/software/make/manual/make.html 一篇文章: 假设我们有一个程序由5个文件组成,源代码如下:/*main.c ...

  10. Zookeeper核心机制

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) Zookeeper是Hado ...