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

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

定义合法的括号序列如下:

1 空序列是一个合法的序列

2 如果S是合法的序列,则(S)和[S]也是合法的序列

3 如果A和B是合法的序列,则AB也是合法的序列

例如:下面的都是合法的括号序列

(),  [],  (()),  ([]),  ()[],  ()[()]

下面的都是非法的括号序列

(,  [,  ),  )(,  ([)],  ([(]

给定一个由'(',  ')',  '[', 和 ']' 组成的序列,找出以该序列为子序列的最短合法序列。

d[i][j]为输入序列从下标i到下标j最少需要加多少括号才能成为合法序列。0<=i<=j<len (len为输入序列的长度)。

c[i][j]为输入序列从下标i到下标j的断开位置,如果没有断开则为-1。

当i==j时,d[i][j]为1

当s[i]=='(' && s[j]==')' 或者 s[i]=='[' && s[j]==']'时,d[i][j]=d[i+1][j-1]

否则d[i][j]=min{d[i][k]+d[k+1][j]} i<=k<j ,  c[i][j]记录断开的位置k

采用递推方式计算d[i][j]

输出结果时采用递归方式输出print(0, len-1)

输出函数定义为print(int i, int j),表示输出从下标i到下标j的合法序列

当i>j时,直接返回,不需要输出

当i==j时,d[i][j]为1,至少要加一个括号,如果s[i]为'(' 或者')',输出"()",否则输出"[]"

当i>j时,如果c[i][j]>=0,说明从i到j断开了,则递归调用print(i, c[i][j]);和print(c[i][j]+1, j);

如果c[i][j]<0,说明没有断开,如果s[i]=='(' 则输出'('、 print(i+1, j-1); 和")"  否则输出"[" print(i+1, j-1);和"]"

#include <cstdio>
#include <cstring> using namespace std; int dp[107][107];
int c[107][107];
char ch[107];
int len ; void mydp()
{
memset(dp,0,sizeof(dp));
memset(c,0,sizeof(c)); int i,j,k,l;
for(i = 0; i < len; i++) dp[i][i] = 1;
int min;
for(l = 1; l < len; l++)
for(i = 0; i+l < len; i++)
{
j = i+l;
min=dp[i][i] + dp[i+1][j];
c[i][j] = i;
for(k = i+1; k < j; k++)
{
if(dp[i][k]+dp[k+1][j] < min)
{
min = dp[i][k] + dp[k+1][j];
c[i][j] = k;
}
}
dp[i][j] = min; if( (ch[i] == '(' && ch[j] == ')') || (ch[i] == '[' && ch[j] == ']') )
{
if(dp[i+1][j-1] < min){
dp[i][j] = dp[i+1][j-1];
c[i][j] = -1;
}
}
}
} void print(int i,int j)
{
if(i > j) return;
if(i == j)
{
if(ch[i]== '(' || ch[i] == ')') printf("()");
else printf("[]");
}
else
{
if(c[i][j] >= 0)
{
print(i,c[i][j]);
print(c[i][j]+1,j);
}
else
{
if(ch[i] == '(')
{
printf("(");
print(i+1,j-1);
printf(")");
}
else
{
printf("[");
print(i+1,j-1);
printf("]");
}
}
}
} void printdp()
{
for(int i = 0; i < len; i++)
{for(int j = 0; j < len; j++)
printf("%d ",dp[i][j]);
printf("\n");}
printf("\n");
for(int i = 0; i < len; i++)
{for(int j = 0; j < len; j++)
printf("%d ",c[i][j]);
printf("\n");}
}
int main()
{
scanf("%s",ch);
len = strlen(ch);
mydp();
// printdp();
print(0,len-1);
printf("\n"); return 0;
}

  

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. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

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

  5. 区间DP POJ 1141 Brackets Sequence

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

  6. POJ 1141 Brackets Sequence (区间DP)

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

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

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

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

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

  9. POJ 1141 Brackets Sequence

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

  10. Poj 2955 brackets(区间dp)

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

随机推荐

  1. currentStyle、getComputedStyle

    element.offsetWidth: 返回元素的宽度,包括边框和内边距. element.offsetHeight: 返回元素的高度,包括边框和内边距. currentStyle: 获取计算后的样 ...

  2. python中调用C++写的动态库

    一.环境:Windows XP + Python3.2 1. dll对应的源文件(m.cpp): #include <stdio.h> extern "C" { _de ...

  3. JSP(一)

    开宗明义:JSP本质上就是一个Servlet scriplet JSP 变量和函数的声明 局部变量 <% int a = 3;> 全局变量和函数 <%! int a = 3;> ...

  4. 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求

    在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求.Octivi联合创始人兼软件架构师Antoni Orfin将向你介绍一个非常简单的架构,使用H ...

  5. LinuxCmd

    Q1.关掉字符界面下的屏保 A:setterm -blank 0 Q2.top Top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. ...

  6. PBOC/EMV 中SDA和DDA简介

    PBOC/EMV里有两个非常重要的概念,SDA(static data authentication)和DDA(dynamic data authentication),分别叫做静态数据认证和动态数据 ...

  7. Asp.Net MVC4配置Ext.Net

    首先,下载MVC用的DLL包(Ext.NET.MVC.Pro.2.2.0.zip).讲DLL文件拷贝到工程bin目录下,引用进工程. 第二步,配置Views文件夹下的web.config文件(具体文字 ...

  8. [开源]在iOS上实现Android风格的控件Toast

    [开源]在iOS上实现Android风格的控件Toast iOS的风格和Apple其他产品一样,简单而粗暴.没有给人其他选择的余地,让你又爱又恨.同样的,Apple对待iOS平台的开发人员和对待大众消 ...

  9. EF 6 调用存储过程时返回多结果集和OUTPUT参数问题

    原文地址:http://q.cnblogs.com/q/56836/ 各位大侠,提问一个关于EF6调用存储过程时返回多结果集和OUTPUT参数问题 目前已经可以调用存储过程并且可以返回多个结果集. 但 ...

  10. c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow

    c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow How to use wst ...