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

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

如果有空串要用gets,scanf不能处理空串

  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <stack>
  9. #include <queue>
  10. #include <cctype>
  11. #include <vector>
  12. #include <iterator>
  13. #include <set>
  14. #include <map>
  15. #include <sstream>
  16. using namespace std;
  17.  
  18. #define mem(a,b) memset(a,b,sizeof(a))
  19. #define pf printf
  20. #define sf scanf
  21. #define spf sprintf
  22. #define pb push_back
  23. #define debug printf("!\n")
  24. #define INF 10000
  25. #define MAX(a,b) a>b?a:b
  26. #define blank pf("\n")
  27. #define LL long long
  28. #define ALL(x) x.begin(),x.end()
  29. #define INS(x) inserter(x,x.begin())
  30. #define pqueue priority_queue
  31.  
  32. const int MAXN = + ;
  33.  
  34. int n,m;
  35.  
  36. int dp[][],path[][];
  37. char a[];
  38.  
  39. bool match(int i,int j)
  40. {
  41. return (a[i]=='(' && a[j]== ')') || (a[i]=='[' && a[j]== ']');
  42. }
  43. void print(int i,int j)
  44. {
  45. if(i>j) return;
  46. if(i == j)
  47. {
  48. if(a[i] == '(' || a[i]== ')')
  49. pf("()");
  50. else
  51. pf("[]");
  52. return;
  53. }
  54. if(path[i][j] == -)
  55. {
  56. pf("%c",a[i]);
  57. print(i+,j-);
  58. pf("%c",a[j]);
  59. return;
  60. }
  61. else
  62. {
  63. print(i,path[i][j]);
  64. print(path[i][j]+,j);
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. int i,j;
  71. while(gets(a))
  72. {
  73. n = strlen(a);
  74. mem(dp,);
  75. mem(path,);
  76. for(i=;i<n;i++)
  77. dp[i][i]=;
  78. for(int l = ;l<n;l++)
  79. {
  80. for(i=;i<n-l;i++)
  81. {
  82. j = i+l;
  83. dp[i][j] = <<;
  84. if(match(i,j) && dp[i+][j-] < dp[i][j])
  85. {
  86. dp[i][j] = dp[i+][j-];
  87. path[i][j] = -;
  88. }
  89.  
  90. for(int k =i;k<j;k++)
  91. {
  92. if(dp[i][k]+dp[k+][j] < dp[i][j])
  93. {
  94. dp[i][j] = dp[i][k]+dp[k+][j];
  95. path[i][j] = k;
  96. }
  97. }
  98. }
  99. }
  100. print(,n-);
  101. blank;
  102. }
  103. return ;
  104. }

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. freemarker模板跟java代码放在一起

    在配置 freemarkerConfig时加上 <property name="templateLoaderPath" value="classpath:hello ...

  2. jxl读取excel

    String path=""; String path2=""; File file = new File(path); File file2 = new Fi ...

  3. sql 列集合

    STUFF((SELECT ','+CAST( TYZ_Bh  as varchar(10)) FROM #1 where 片区划分='江东' for xml path('')),1,1,'')

  4. CDQZ Day2

    模拟题 day2出题人: liu_runda题目名称 一盘大棋 下一代互联网 强连通分量源程序文件名 chess.cpp net.cpp scc.cpp输入文件名 chess.in net.in sc ...

  5. 论文阅读 | ExtremeNet:Bottom-up Object Detection by Grouping Extreme and Center Points

    相关链接 论文地址:https://arxiv.org/abs/1901.08043 论文代码:https://github.com/xingyizhou/ExtremeNet 概述 ExtremeN ...

  6. Locust HTTP client

    Http client 默认是以安全模式运行的,任何由于连接错误.超时或者类似错误引起的异常,都会返回一个空的Response对象,这个请求将会再locust统计中标记为failure,返回的虚拟对象 ...

  7. 20190430-Bootstrapの组件

    写在前面的乱七八糟:今天务必要把BT盘完~任重道远~ 目录 1.字体图标 2.下拉菜单 3.按钮组 4.输入框组 5.导航 5.1标签页 5.2胶囊式标签页 5.3路径导航/面包屑导航 6.导航条 7 ...

  8. vscode安装golang插件失败问题

    vscode安装golang插件失败问题 dlv go-outline go-symbols gocode-gomod gocode 代码补全 godef 代码跳转 golint gopkgs gor ...

  9. Service启动流程

    Service启动流程从整个宏观上来看,它的模型如下 startService启动流程时序图 Activity中使用的startService方法是定义在Context的抽象类中,它的真正实现者是Co ...

  10. python namedtuple命名元组

    from collections import namedtuple Animal=namedtuple('Animal','name age type') perry=Animal(name='pe ...