POJ1141 Brackets Sequence
Description
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
正解:DP
解题报告:
DP题,乍一看我居然不会做,也是醉了。开始想用贪心水过,发现会gi烂。
详细博客:http://blog.csdn.net/lijiecsu/article/details/7589877
不详细说了,见代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = ;
char ch[MAXN];
int l;
int f[MAXN][MAXN],c[MAXN][MAXN]; inline void output(int l,int r){
if(l>r) return ;
if(l==r) {
if(ch[l]=='(' || ch[l]==')') printf("()");
else printf("[]");
}
else{
if(c[l][r]>=) {
output(l,c[l][r]);
output(c[l][r]+,r);
}
else{
if(ch[l]=='(') {
printf("(");
output(l+,r-);
printf(")");
}
else{
printf("[");
output(l+,r-);
printf("]");
}
}
}
} inline void solve(){
scanf("%s",ch);
int len=strlen(ch);
for(int i=;i<len;i++) f[i][i]=;
for(int i=;i<len;i++) for(int j=;j<len;j++) c[i][j]=-;
for(int l=;l<=len-;l++)
for(int i=;i+l<=len-;i++){
int j=i+l;
int minl=f[i][i]+f[i+][j];
c[i][j]=i;
for(int k=i+;k<j;k++){
if(minl>f[i][k]+f[k+][j]) {
minl=f[i][k]+f[k+][j];
c[i][j]=k;
}
}
f[i][j]=minl; if(( ch[i]=='(' && ch[j]==')' ) || ( ch[i]=='[' && ch[j]==']' )) {
if(f[i][j]>f[i+][j-]) {
f[i][j]=f[i+][j-];
c[i][j]=-;
}
}
} output(,len-);
printf("\n");
} int main()
{
solve();
return ;
}
POJ1141 Brackets Sequence的更多相关文章
- [原]POJ1141 Brackets Sequence (dp动态规划,递归)
本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ...
- 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 ...
- 记忆化搜索(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 ...
随机推荐
- box unboxing(装箱 拆箱) C#编程指南
box(装箱)消耗大 box在堆栈中创建一个新的对象,性能消耗大 int i = 123; // Boxing copies the value of i into object o. object ...
- NGUI学习笔记汇总
NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...
- Jenkins学习八:Jenkins语言本地化
在Jenkins中,英语一大片,不懂英语的看着头疼.非常高兴的是,Jenkins作为一个主流流行的持续构建工具,提供了一个本地化语言的配置界面. 你可以找到它,在Jenkins每页的左下角.如下图: ...
- java 21-11 数据输入、输出流和内存操作流
IO数据流: 可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in) 数据输出流:DataOutputStream ...
- C语言 三级指针的应用
//三级指针的使用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #includ ...
- [C#] 走进异步编程的世界 - 开始接触 async/await(转)
原文链接:http://www.cnblogs.com/liqingwen/p/5831951.html 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- 信息安全系统设计基础实验一 20135211&20135216
北京电子科技学院(BESTI) 实 验 报 告 封面 课程:信息安全系统设计基础 班级:1352 姓名:(按贡献大小排名)李行之 刘蔚然 ...
- Struts2 面试题分析
1. 简述 Struts2 的工作流程: ①. 请求发送给 StrutsPrepareAndExecuteFilter ②. StrutsPrepareAndExecuteFilter 判定该请求是否 ...
- IOS开发之——保存图片到相册的功能实现
//保存 UIButton *saveBtn = [[UIButton alloc] init]; // saveBtn.frame = CGRectMake((screenWi ...