题意:

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

分析:

非常经典,以前做过相似一道题,用区间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. PowerDesigner修改设计图中文字的字体大小等样式

    设计图中默认的字体是对英文比较合适的,中文就看不清楚了,特别不美观.但是可以通过修改“Display Preferences”适应我们的汉字. 我使用的PowerDesigner版本是15.1(测试版 ...

  2. @QueryParam和@PathParam比较

    来源:http://jackyrong.iteye.com/blog/1128364 1 先来看@queryparam Path("/users") public class Us ...

  3. URLEncode转json

    http://tool.chinaz.com/tools/urlencode.aspx?jdfwkey=zobsn2 http://www.bejson.com/

  4. 用javascript编写的简单计算器

    老师布置的任务,弄了一天,总算把代码搞出来了,作为菜鸟给自己一点小掌声,传说中JS是很简单的,但是要写出一个程序真的很需要锻炼,我的锻炼实在是太少了,天天DOTA中.呵呵(做了些小注释)大家应该能看懂 ...

  5. Android:让EditText不自动获取焦点

    解决方法: 在EditText的父级控件中加入属性: android:focusable="true" android:focusableInTouchMode="tru ...

  6. Google Hacking技术

    (1)google hack database https://www.exploit-db.com/google-hacking-database/ (2)查找包含某关键字的特定类型文件 命令:关键 ...

  7. win8系统 Reflect 破解

    在win8系统中,从网上下载Reflect和注册机,按照教程操作,但是在生成应答字符串的时候,没有生成字符串,很纳闷. 尝试了好几个版本,最终是在win7系统中也按照流程操作一遍.重点是在生成应答字符 ...

  8. Apple开发者账号申请学习方式

    http://jingyan.baidu.com/article/414eccf610e7c76b431f0a94.html https://developer.apple.com/wwdc/sche ...

  9. ios跳转

    目标应用程序:打开info.plist,添加一项URL types展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme展开URL Schem ...

  10. R语言日期时间函数

    Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...