题目链接: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. BZOJ4285: 使者

    搞出dfs序,转化为查询矩形点数,树套树搞定. #include<cstdio> #include<cstdlib> #define N 100005 #define IF e ...

  2. 机器学习中的相似性度量(Similarity Measurement)

    机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...

  3. Another app is currently holding the yum lock

    摘要 在使用yum安装的时候,出现该error. 错误 Another app is currently holding the yum lock; waiting for it to exit... ...

  4. 最大公共字串LCS问题(阿里巴巴)

    给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...

  5. 【转】搞不清FastCgi与php-fpm之间是个什么样的关系?

    我在网上查fastcgi与php-fpm的关系,查了快一周了,基本看了个遍,真是众说纷纭,没一个权威性的定义. 网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-f ...

  6. c#微信开发 转

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...

  7. I18N 国际化

    http://blog.sina.com.cn/s/blog_6c7e59770101p7w9.html 一.I18N 在 J2EE 中的应用 [转载:http://blog.csdn.net/cha ...

  8. phpcms中的RBAC权限系统

    PHPCMS中的RBAC权限系统主要用到了4张数据表:管理员表,角色表,菜单表,菜单权限表.先来看看数据库的数据表结构: admin 管理员表 ID 字段 类型 Null 默认 索引 额外 注释 1 ...

  9. 跟着百度学PHP[5]函数篇1-参数

    ps:俺的文章俺懂就行.大家不要拿来学习不然每个人的学习思路不一样.看视屏文章的老师不同笔记不同加上我也是新手上路,还怕误导!请大家看行且思考,帮忙挖Bug也可以. 案例要求:你可以写一个两行三列的表 ...

  10. 音频指纹(Philips)

    参考<A Highly Robust Audio Fingerprinting System> Philips 音频指纹提取流程: 仿真效果: 第一个图为歌曲1的第一个指纹. 第二个图为歌 ...