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 ...
随机推荐
- E20171214-sl
well-grounded 地基打的好 relevent adj. 有关的,中肯的; 相关联的 proposal n. 建议; 提议; 求婚; 〈美〉投标;
- Android框架式编程之EasyPermissions
EasyPermission库是一个谷歌官方提供的简化基本的系统权限逻辑的库,可用于在Android M或者更高版本上. 官方项目地址:https://github.com/googlesamples ...
- 乐字节Java8核心特性之方法引用
大家好,我是乐字节的小乐,上一次我们说到了Java8核心特性之函数式接口,接下来我们继续了解Java8又一核心特性--方法引用. Java8 中引入方法引用新特性,用于简化应用对象方法的调用, 方法引 ...
- 【题解】PIE [POI2015] [P3585]
[题解]\(PIE\) \([POI2015]\) \([P3585]\) 逼自己每天一道模拟题 传送门:\(PIE\) \([POI2015]\) \([P3585]\) [题目描述] 一张 \(n ...
- QuartzJobs 如何发布服务
http://www.cnblogs.com/jys509/p/4614975.html http://www.cnblogs.com/lc-chenlong/p/3948760.html 安装:To ...
- [转]ASP.NET MVC的帮助类HtmlHelper和UrlHelper
本文转自:http://www.cnblogs.com/greatandforever/archive/2010/04/20/1715914.html?login=1 在ASP.NET MVC框架中没 ...
- js易混API汇总
一:slice()方法 ————————————http://www.w3school.com.cn/jsref/jsref_slice_string.asp ———————————————————— ...
- Ray Wenderlich 的 Objective-C编码规范
由于我正在准备模仿饿了么这个app,到时可能有些iOS开发者参与进来.这时如果每个人的Objective-C编码风格都不一样,这样不易于保持代码一致性和难以Code Review.所以我在网上搜索到 ...
- ES6:Generator函数(1)
Generator函数是ES6提供的一种异步编程解决方案.它会返回一个遍历器对象 function* helloWorldGenerator(){ yield “hello”; yield “worl ...
- Linux 学习(四)
搭建jdk 安装jdk操作: 1.光驱挂载:mount /dev/cdrom /mnt 2.拷贝安装包至其他文件夹(如home目录下) 3.执行安装包(bin包:./包名) 4.配置环境变量:打开文件 ...