Brackets Sequence

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 1141
64-bit integer IO format: %lld      Java class name: Main

Special Judge
 
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

 
解题:这dp啊,我这种学渣啊,每做一次,就有一次新的感觉!水好深啊!
 
注意是单样例的!改成多样例,立马WA了
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int dp[maxn][maxn],c[maxn][maxn] = {-};
char str[maxn];
void print(int i,int j) {
if(i > j) return;
if(i == j) {
if(str[i] == '(' || str[j] == ')')
printf("()");
else printf("[]");
} else {
if(c[i][j] >= ) {
print(i,c[i][j]);
print(c[i][j]+,j);
} else {
if(str[i] == '(') {
printf("(");
print(i+,j-);
printf(")");
} else {
printf("[");
print(i+,j-);
printf("]");
}
}
}
}
void go() {
int len = strlen(str),i,j,k,theMin,t;
for(i = ; i < len; i++) dp[i][i] = ;
for(k = ; k < len; k++) {
for(i = ; i+k < len; i++) {
j = i+k;
theMin = dp[i][i]+dp[i+][j];
c[i][j] = i;
for(t = i+; t < j; t++) {
if(dp[i][t]+dp[t+][j] < theMin) {
theMin = dp[i][t]+dp[t+][j];
c[i][j] = t;
}
}
dp[i][j] = theMin;
if(str[i] == '(' && str[j] == ')' || str[i] == '[' && str[j] == ']') {
if(dp[i+][j-] < theMin) {
dp[i][j] = dp[i+][j-];
c[i][j] = -;
}
}
}
}
print(,len-);
}
int main() {
scanf("%s",str);
go();
puts("");
return ;
}

BNUOJ 1260 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 ...

随机推荐

  1. [App Store Connect帮助]六、测试 Beta 版本(4.3) 管理 Beta 版构建版本:为 Beta 版构建版本提供出口合规证明

    如果您没有完成出口合规证明,则该 Beta 版构建版本的状态为“缺少合规证明”.您可以在 TestFlight 部分中回答必需的出口合规证明问题. 必要职能:“帐户持有人”职能.“管理”职能或“App ...

  2. javascript实现继承的4种方法,以及它们的优缺点

    1. 原型链继承(有缺陷): 缺陷1:切断了Zi.prototype.constructor与Zi的关系 缺陷2:原型链上的引用类型的数据会被所有实例共享 2. 构造函数继承(有缺陷): 缺陷1:Fu ...

  3. mysql left join 出现的结果会重复

    left join 基本用法 MySQL left join 语句格式 A LEFT JOIN B ON 条件表达式 left join 是以A表为基础,A表即左表,B表即右表. 左表(A)的记录会全 ...

  4. thinkphp5 分页 paginate

    tp5分页带参数的时候,用到 paginate 后面的几个参数 paginate有三个参数, 第一个必须表是每页分多少个[如果配置文件中设置了,可以不用] 第二个参数表是的是简洁分页,如果为true, ...

  5. GIT学习之路第四天 远程仓库

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 git的服务器---Github,自行注册github账号后,按下面的步骤操作: 第一步,事实上,本地Git仓库和Github仓库之间的传输是 ...

  6. Java 创建Excel并逐行写入数据

    package com.xxx.common.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  7. offset家族基本简介

    Offset家族简介 offset这个单词本身是--偏移,补偿,位移的意思. js中有一套方便的获取元素尺寸的办法就是offset家族: offsetWidth和offsetHight 以及offse ...

  8. BPI-M1P(全志A20)刷Android启动卡之后启动的过程

    http://blog.csdn.net/wb4916/article/details/78031511BPI-M1P(全志A20)刷Android启动卡之后启动的过程 BPI-M1P(全志A20)刷 ...

  9. 解决opencv在pycharm中无代码自动提示的bug

    2018-03-0422:19:39 首先,估计这不是bug 可能是我自己误操作导致的,但是让我搞了好久才搞定,实在是苦恼 如图已实现功能,百度里有很多朋友出现了,这个无代码提示的问题 大概是这样的, ...

  10. Python学习笔记之默认参数

    函数定义时 参数定义的顺序必须是:必选参数.默认参数.可变参数和关键字参数. def test(a,b,c=1,*d,**e) pass