题目链接

很早 很早之前就看过的一题,今天终于A了。状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的。。二了,直接输出就过了。。

 #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[][];
int flag[];
int pos[];
char str[];
int dfs(int L,int R)
{
int i,minz;
if(dp[L][R])
return dp[L][R];
if(L > R)
return dp[L][R] = ;
if(L == R)
return dp[L][R] = ;
if(str[L] == ']'||str[L] == ')')
return dp[L][R] = + dfs(L+,R);
minz = ;
for(i = L; i <= R; i ++)
{
if(str[L] == '('&&str[i] == ')')
minz = min(minz, + dfs(L+,i-) + dfs(i+,R));
else if(str[L] == '['&&str[i] == ']')
minz = min(minz, + dfs(L+,i-) + dfs(i+,R));
else
minz = min(minz, + dfs(L+,i) + dfs(i+,R));
}
return dp[L][R] = minz;
}
void find(int L,int R,int ans)
{
int i;
if(L > R)
return ;
if(L == R)
{
if(str[L] == ')')
printf("()");
else if(str[L] == ']')
printf("[]");
else if(str[L] == '(')
printf("()");
else if(str[L] == '[')
printf("[]");
return ;
}
if(str[L] == ']'||str[L] == ')')
{
if(str[L] == ')')
printf("()");
else if(str[L] == ']')
printf("[]");
find(L+,R,ans-);
return ;
}
for(i = L; i <= R; i ++)
{
if(str[L] == '('&&str[i] == ')')
{
if(ans == + dp[L+][i-] + dp[i+][R])
{
printf("(");
find(L+,i-,dp[L+][i-]);
printf(")");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '['&&str[i] == ']')
{
if(ans == + dp[L+][i-] + dp[i+][R])
{
printf("[");
find(L+,i-,dp[L+][i-]);
printf("]");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '[')
{
if(ans == + dp[L+][i] + dp[i+][R])
{
printf("[");
find(L+,i,dp[L+][i]);
printf("]");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '(')
{
if(ans == + dp[L+][i] + dp[i+][R])
{
printf("(");
find(L+,i,dp[L+][i]);
printf(")");
find(i+,R,dp[i+][R]);
return ;
}
}
}
return ;
}
int main()
{
int len,ans;
scanf("%s",str);
len = strlen(str);
ans = dfs(,len-);
find(,len-,ans);
printf("\n");
return ;
}

POJ 1141 Brackets Sequence(DP)的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

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

  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, DP打印路径)

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

  4. POJ 1141 Brackets Sequence (区间DP)

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

  5. POJ 1141 Brackets Sequence

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

  6. poj 1141 Brackets Sequence (区间dp)

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

  7. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

  8. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

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

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

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

随机推荐

  1. python中最简单的多进程程序

    学着.. #!/usr/bin/env python # -*- coding: utf-8 -*- # Spawn a Process: Chapter 3: Process Based Paral ...

  2. HDU4495 Rectangle

    求组成的等腰三角形面积最大值. 对此题的总结:暴力出奇迹 组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先 ...

  3. android 入门-基础了解

    strings.xml – 文字資源. colors.xml – 顏色資源. dimens.xml – 尺寸資源. arrays.xml – 陣列資源. styles.xml – 樣式資源. #RGB ...

  4. 接口JSon字符串格式

  5. Android adb的使用

    参考:http://blog.csdn.net/veryitman/article/details/6437090 1. 进入shell 进入设备shell adb shell 2. 安装 apk & ...

  6. Is WPFdead

    最近看到一个bog.http://www.codeproject.com/Articles/818281/Is-WPF-dead-the-present-and-future-of-WPF 大体上讲了 ...

  7. 程序员必懂:javaweb三大框架知识点总结

    原文链接:http://www.cnblogs.com/SXTkaifa/p/5968631.html javaweb三大框架知识点总结 一.Struts2的总结 1.Struts 2的工作流程,从请 ...

  8. mybatis 加载配置文件的两种方式

    package com.atguigu.day03_mybaits.test; import java.io.IOException;import java.io.InputStream;import ...

  9. C语言连接SQLSERVER数据库

    第一步:配置ODBC.在配置ODBC时有用户DSN.系统DSN.和文件DSN三种方法,为了稳妥起见,采用系统DSN. DSN的名字叫LocalServer,帐号:sa,密码123456 第二步:打开V ...

  10. Ajax实现点击省份显示相应城市

    功能:不用级联效果,自己写ajax,从接口读取省份城市数据,实现点击省份显示相应城市.后端根据省份ID,给前端返回城市. 一.DOM结构(套用blade模板) <div class=" ...