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. 在Linux环境下使用OpenSSL对消息和文件进行加密(转载)

    转自:http://netsecurity.51cto.com/art/201301/378513.htm 1.简介 OpenSSL是一款功能强大的加密工具包.我们当中许多人已经在使用OpenSSL, ...

  2. 慕课网4-2 编程练习:jQuery祖先后代选择器小案例

    4-2 编程练习 结合所学的祖先后代选择器,实现如下图所示效果 任务 (1)使用祖先后代选择器将第二段文字背景色变成红色 (2)使用jQuery的.css()方法设置样式,语法css('属性 '属性值 ...

  3. Java多线程(五)停止线程 interrupt

    调用interrupt方法仅仅是在当前线程中打了一个停止的标记,并不是真正停止线程. this.interrupted() :测试当前线程是否已经中断,执行后具有将状态标志清除为false的功能 is ...

  4. STL---vector的内存分配策略

    2级策略,过程如下: 第一级 __malloc_alloc_template内存分配器 该分配器是对malloc.realloc以及free的封装: 第二级  __default_alloc_temp ...

  5. 重新学习Java——对象和类(一)

    之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...

  6. [ CodeForces 1064 B ] Equations of Mathematical Magic

    \(\\\) \(Description\) \(T\) 组询问,每次给出一个 \(a\),求方程 \[ a-(a\oplus x)-x=0 \] 的方案数. \(T\le 10^3,a\le 2^{ ...

  7. CF868B Race Against Time

    思路: 模拟.少写了一个等号FST了,好气啊. 实现: #include <bits/stdc++.h> using namespace std; int main() { int h, ...

  8. servlet——web应用中路径问题

    target.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...

  9. 设置bootstrap modal模态框的宽度和宽度

    (1)修改宽度可以通过修改modal中的modal-dialog这个div宽度实现 <div class="modal-dialog" style="width:6 ...

  10. (转)淘淘商城系列——导入商品数据到索引库——Service层

    http://blog.csdn.net/yerenyuan_pku/article/details/72894187 通过上文的学习,我相信大家已经学会了如何使用Solrj来操作索引库.本文我们将把 ...