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. mysqli扩展库---------预处理技术

    1, PHP程序与mysql之间处理sql语句流程如下,减少执行时间方式有三种: ① 减少php发送sql次数: ② 减少php与mysql之间网络传输时间: ③ 减少mysql的编译时间: 2, 预 ...

  2. MySQL LOAD DATA

    <?php /** * @Author: Awe * @Date: 2016-10-26 17:26:54 * @Last Modified by: Awe * @Last Modified t ...

  3. P4383 [八省联考2018]林克卡特树lct 树形DP+凸优化/带权二分

    $ \color{#0066ff}{ 题目描述 }$ 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的 ...

  4. 返回参数去掉xml格式,以纯json格式返回(转)

    Json 格式显示public static void Register(HttpConfiguration config) { //////////////设置不以xml格式返回 config.Fo ...

  5. MYSQL ERROR 1049 (42000): Unknown database

    https://www.cnblogs.com/hedgehog105/p/10196566.html lower_case_table_names=2

  6. CTF<密码学> writeup 传统知识+古典密码

    小明某一天收到一封密信,信中写了几个不同的年份          辛卯,癸巳,丙戌,辛未,庚辰,癸酉,己卯,癸巳. 信的背面还写有“+甲子”,请解出这段密文. key值:CTF{XXX} 百度可以查到 ...

  7. git特殊用法

    git stash的使用 1.当前分支写了一半的代码,需要切到其他分支修复bug或者完成优先级较高的任务时 git stash 暂存分支进度 git stash list 查看草稿区 git stas ...

  8. WorldCount 结对项目

    合作者:201631062501,201631062129 代码地址:https://gitee.com/guilinyunya/WorldCount 伙伴博客地址:https://www.cnblo ...

  9. springboot 接口返回数据时 net.sf.json.JSONNull["empty"]) 异常

    @ResetController返回数据时出现异常 Could not write JSON: Object is null; nested exception is com.fasterxml.ja ...

  10. Oracle DMP

    通过DMP对Oracle数据库进行导入导出 打开“开始”--->输入cmd,打开cmd命令窗口,输入以下命令即可 1导出 (1)将数据库ORACLE完全导出,用户名system密码manager ...