BNUOJ 1260 Brackets Sequence
Brackets Sequence
This problem will be judged on PKU. Original ID: 1141
64-bit integer IO format: %lld Java class name: Main
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
Output
Sample Input
([(]
Sample Output
()[()]
Source
#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的更多相关文章
- POJ 题目1141 Brackets Sequence(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- POJ1141 Brackets Sequence
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- ZOJ1463:Brackets Sequence(间隙DP)
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- CSUOJ 1271 Brackets Sequence 括号匹配
Description ]. Output For each test case, print how many places there are, into which you insert a ' ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
随机推荐
- source命令用法(转载)
转自:http://zhidao.baidu.com/link?url=mNfsPHSjTEm7llgyMYx0UVNwkJmD_cxLeHtZnHcM6Ms8LDXofVHka_EzHi6GltbR ...
- MyBatis基本应用
框架的概念: 框架(Framework)是一个提供了可重用的公共结构的半成品. 数据持久化: 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称. ORM(Obj ...
- MARK ZUCKERBERG, A letter to our daughter(转)
A letter to our daughter MARK ZUCKERBERG·WEDNESDAY, DECEMBER 2, 2015 Dear Max, Your mother and I ...
- bzoj 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱【区间dp】
就是区间dp啦f[i][j]表示以i开头的长为j+1的一段的答案,转移是f[i][j]=s[i+l]-s[i-1]+min(f[i][j-1],f[i+1][j-1]),初始是f[i][1]=a[i] ...
- python之Beautiflusoup操作
from bs4 import BeautifulSoupimport requestsimport os ######对风景进行爬出操作r = requests.get("http://6 ...
- java虚拟机全集(31篇文章)
深入理解java虚拟机系列 深入理解Java虚拟机笔记---内存区域 深入理解Java虚拟机笔记---判断对象是否存活 深入理解Java虚拟机笔记---垃圾收集算法 深入理解Java虚拟机笔记---垃 ...
- centos 安装sysbench
安装sysbench 下载并且解压 shell> wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbe ...
- 数据结构之动态顺序表(C实现)
线性表有2种,分为顺序表和链表. 顺序表: 采用顺序存储方式,在一组地址连续的存储空间上存储数据元素的线性表(长度固定) 链表: 有3种,单链表.双向链表.循环链表(长度不固定) seqList.h ...
- [CREC2007/CQOI2014]robotic sort
Description 一个实验室里有n个长短不一的试管.你的任务是编写一段程序,用机器臂把它们按照高度从小到大的顺序排列. 对于高度相同的试管,排序前后的相对位置应保持不变.排序方法如图所示. 排序 ...
- 用Movie显示gif(1)SimpleGif
代码如下: import android.content.Context; import android.graphics.Canvas; import android.graphics.Movie; ...