题目链接:http://poj.org/problem?id=1141

题解:求已知子串最短的括号完备的全序列

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e2+;
const int INF=0x3f3f3f3f; int dp[][];
int pos[][];
char str[]; int Find(int x,int y)
{
if((str[x]=='(' && str[y]==')') || (str[x]=='[' && str[y]==']')) return ;
else return ;
} void print(int x,int y)
{
if(x>y) return;
if(x==y)
{
if(str[x]=='[' || str[x]==']') printf("[]");
else printf("()");
}
else
{
if(pos[x][y]>=)
{
print(x,pos[x][y]);
print(pos[x][y]+,y);
}
else if(str[x]=='(')
{
printf("(");
print(x+,y-);
printf(")");
}
else
{
printf("[");
print(x+,y-);
printf("]");
}
}
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%s",str+);
memset(pos,-,sizeof(pos));
memset(dp,,sizeof(dp));
int n=strlen(str+);
for(int i=; i<=n; i++) dp[i][i]=;
for(int d=; d<n; d++)
{
for(int i=; i+d<=n; i++)
{
int j=i+d;
dp[i][j]=INF;
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];
pos[i][j]=k;
}
}
if( Find(i,j) && dp[i+][j-]<dp[i][j] )
{
dp[i][j]=dp[i+][j-];
pos[i][j]=-;
}
}
}
print(,n);
puts("");
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://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...

  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. Caused by: java.sql.BatchUpdateException: Transaction error, need to rollback. errno:1205 Lock wait timeout exceeded; try restarting transaction

    更新的时候报 Caused by: java.sql.BatchUpdateException: Transaction error, need to rollback. errno:1205 Loc ...

  2. TAC Beta版本 冲冲冲!!!

    一.Beta版本冲刺博客目录: 第一天 第二天 第三天 第四天 第五天 第六天 第七天 二.Beta版本需要改进完善的功能: service层传入参数的判断与提示以及各函数内的相应提示 界面改进.优化 ...

  3. java 判断String 是否为空

    StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ...

  4. 学习Javascript

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  5. 挣值管理(PV、EV、AC、SV、CV、SPI、CPI) 记忆

    挣值管理法中的PV.EV.AC.SV.CV.SPI.CPI这些英文简写相信把大家都搞得晕头转向的.在挣值管理法中,需要记忆理解的有三个参数:PV.AC.EV.     PV:计划值,在即定时间点前计划 ...

  6. 统计SqlServer每张表内的数据量

    CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)EXEC sp_MSforeachtable 'INSERT INTO #temp SE ...

  7. 关于js中的时间处理

    关于js编程, 主要是, 绝大部分是用 jquery. 但是, js原生的一些方法和属性也是要掌握的, 这个只是在 遇到的时候, 记一下就好了, 如: event的relatedTarget属性: 主 ...

  8. service和serviceImpl的选择

    同行中,有些同行公司的代码风格是service层=service接口+serviceImpl实现类: 而有的同行公司的代码风格是service层=service类: 为什么不一样呢? 以前没想过这个问 ...

  9. [Machine Learning] Learning to rank算法简介

    声明:以下内容根据潘的博客和crackcell's dustbin进行整理,尊重原著,向两位作者致谢! 1 现有的排序模型 排序(Ranking)一直是信息检索的核心研究问题,有大量的成熟的方法,主要 ...

  10. html页面的CSS、DIV命名规则

    CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...