http://blog.csdn.net/cc_again/article/details/10169643

http://blog.csdn.net/lijiecsu/article/details/7589877

如果有空串要用gets,scanf不能处理空串

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue const int MAXN = + ; int n,m; int dp[][],path[][];
char a[]; bool match(int i,int j)
{
return (a[i]=='(' && a[j]== ')') || (a[i]=='[' && a[j]== ']');
}
void print(int i,int j)
{
if(i>j) return;
if(i == j)
{
if(a[i] == '(' || a[i]== ')')
pf("()");
else
pf("[]");
return;
}
if(path[i][j] == -)
{
pf("%c",a[i]);
print(i+,j-);
pf("%c",a[j]);
return;
}
else
{
print(i,path[i][j]);
print(path[i][j]+,j);
}
} int main()
{
int i,j;
while(gets(a))
{
n = strlen(a);
mem(dp,);
mem(path,);
for(i=;i<n;i++)
dp[i][i]=;
for(int l = ;l<n;l++)
{
for(i=;i<n-l;i++)
{
j = i+l;
dp[i][j] = <<;
if(match(i,j) && dp[i+][j-] < dp[i][j])
{
dp[i][j] = dp[i+][j-];
path[i][j] = -;
} for(int k =i;k<j;k++)
{
if(dp[i][k]+dp[k+][j] < dp[i][j])
{
dp[i][j] = dp[i][k]+dp[k+][j];
path[i][j] = k;
}
}
}
}
print(,n-);
blank;
}
return ;
}

poj 1141 Brackets Sequence ( 区间dp+输出方案 )的更多相关文章

  1. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  2. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  3. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  4. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  5. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  6. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  7. POJ 2955 Brackets (区间dp入门)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  8. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  9. Poj 2955 brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7795   Accepted: 4136 Descript ...

随机推荐

  1. JQuery实现全选、反选和取消功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 【spring】Spring Boot:定制自己的starter

    概念 在学习Spring Boot的过程中,接触最多的就是starter.可以认为starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring ...

  3. Java 之多态

    Java 之多态(包含封装) 基础知识: Java 在处理基本数据类型(例如int ,char,double)时,都是采用按值传递的方式执行,除此之外的其它类型都是按引用传递的方式执行.对象除了在函数 ...

  4. ArchLinux 下 OpenSSH 高级运用

    00x0.相关介绍 OpenSSH(OpenBSD Secure Shell)使用 SSH 通过计算机网络加密通信的实现. 它是替换由 SSH Communications Security 所提供的 ...

  5. 7,CountDownLatch 与 CyclicBarrier 的 区别

    CountDownLatch : 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDo ...

  6. python 绘制抛物线

    %matplotlib inlineimport matplotlib.pyplot as plt import numpy as npx = range(100) y = [val**2 for v ...

  7. 2018年1月17日总结 css3里transition 和animation 区别

    transition 和animation两个CSS3属性经常被用到实际项目中,想把它整理出来. 1.先介绍transition >>>>>  a. 在做项目中经常会遇见 ...

  8. 2.Bootstrap CSS

    Bootstrap CSS 一.Bootstrap CSS概览 移动设备优先 移动设备优先是 Bootstrap 3 的最显著的变化. 在之前的 Bootstrap 版本中(直到 2.x),您需要手动 ...

  9. 干掉Vivado幺蛾子(1)-- Xilinx Tcl Store

    目录 1. 安装Xilinx Tcl Store 2. 手动更新 2.1 下载库 2.2 修改环境变量 参考文献: 最近在跟着高亚军老师的分析文章来学习Xilinx最近发布的<UltraFast ...

  10. UML-6.3-用例-详述示例

    用例标示:摘要(主成功场景).非正式(几乎所有场景).详述. 下边主要说下详述.其模板: 例子: 用例UC1:处理销售 范围 NextGen POS应用 级别 用户目标 主要参与者 收银员 涉众及关注 ...