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 ...
随机推荐
- centos7的systemd
系统启动流程 POST --> Boot Sequence --> Bootloader --> kernel+initramfs(initrd) --> rootfs --& ...
- MyBatis基本应用
框架的概念: 框架(Framework)是一个提供了可重用的公共结构的半成品. 数据持久化: 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称. ORM(Obj ...
- codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...
- bzoj 1594: [Usaco2008 Jan]猜数游戏【二分+线段树】
写错一个符号多调一小时系列-- 二分答案,然后判断这个二分区间是否合法: 先按值从大到小排序,然后对于值相同的一些区间,如果没有交集则不合法:否则把并集在线段树上打上标记,然后值小于这个值的区间们,如 ...
- 乐字节Java8核心特性之方法引用
大家好,我是乐字节的小乐,上一次我们说到了Java8核心特性之函数式接口,接下来我们继续了解Java8又一核心特性--方法引用. Java8 中引入方法引用新特性,用于简化应用对象方法的调用, 方法引 ...
- 2017青岛网络赛1008 Chinese Zodiac
Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
- 51nod 1166 大数开平方
1166 大数开平方 基准时间限制:4 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 给出一个大整数N,求不大于N的平方根的最大整数.例如:N = 8,2 * 2 ...
- hdu5924Mr. Frog’s Problem
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 解决WebSocket后台报错:The WebSocket session [0] has been closed and no method (apart from close()) may be called on a closed session
场景1: 在后台数据对前台页面进行数据实时推送下,后台采取定时查询数据后,推送给前台页面.在这个过程中,前台页面不停的刷新页面,session在不停的关闭和开启,推送数据时,会出现session连接已 ...
- keystore找回密码
昨天准备给自己的应用发布一个新版本,在apk打包时,发现之前的用的keystore密码忘了. 蛋碎了一地,我把我所能想到的密码都试了一遍(注:我平常在各个门户网站注册基本上用的都是那几个字母和数字组合 ...