个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对

对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越大时很多重复,应该要进行分割,

后面想想又不对,就去看题解了,没想到就是分割,还是动手能力太差,还有思维不够。

  1. for(int j=;j+i<ch.size();j++)
  2. {
  3. if(check(j,j+i))
  4. dp[j][j+i]=dp[j+][j+i-]+;
  5. for(int m=j;m<=j+i;m++)
  6. dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+][j+i]);
  7. }

分割并一次求最大值。动态规划真的是一脸懵逼样,多思考,多瞎想吧,呼~

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

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

while the following character sequences are not:

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

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters ()[, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

  1. ((()))
  2. ()()()
  3. ([]])
  4. )[)(
  5. ([][][)
  6. end

Sample Output

  1. 6
  2. 6
  3. 4
  4. 0
  5. 6
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<iomanip>
  6. #include<string>
  7. #include<algorithm>
  8. using namespace std;
  9. int money[];
  10. int dp[][];
  11. string ch;
  12. const int inf=;
  13. int check(int i,int j){
  14. if((ch[i]=='('&&ch[j]==')')||(ch[i]=='['&&ch[j]==']'))
  15. return ;
  16. return ;
  17. }
  18. void init(){
  19. for(int i=;i<ch.size();i++)
  20. for(int j=;j<ch.size();j++)
  21. dp[i][j]=;
  22. }
  23. int main(){
  24. int n,m;
  25. while(getline(cin,ch,'\n')){
  26. if(ch=="end") break;
  27. init();
  28. for(int k=;k<ch.size()-;k++)
  29. if(check(k,k+))
  30. dp[k][k+]=;
  31. else
  32. dp[k][k+]=;
  33. for(int i=;i<ch.size();i++)
  34. {
  35. for(int j=;j+i<ch.size();j++)
  36. {
  37. if(check(j,j+i))
  38. dp[j][j+i]=dp[j+][j+i-]+;
  39. for(int m=j;m<=j+i;m++)
  40. dp[j][j+i]=max(dp[j][j+i],dp[j][m]+dp[m+][j+i]);
  41. }
  42.  
  43. }
  44. cout<<dp[][ch.size()-]<<endl;
  45. }
  46. return ;
  47. }
  1.  

Brackets (区间DP)的更多相关文章

  1. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  2. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  5. POJ2955:Brackets(区间DP)

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

  6. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  7. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  9. poj 2955 Brackets (区间dp基础题)

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

  10. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. 请求json和xml数据时的方式

    当请求xml数据时,直接通过NSMutableData接收后解析, NSURL *url = [NSURL URLWithString:PATH]; _receiveData = [[NSMutabl ...

  2. 【转载】wget 命令用法详解

    wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点:(1)支持断点下传功能:这一点,也是网络蚂蚁和Fl ...

  3. java 跨数据库导入大数据

    java 跨数据库导入大数据 /** * java程序跨服务器跨数据库批量导入导出百万级数据 * @param args * @throws Exception */ public static vo ...

  4. 在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @

    在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @ 一方面 @符号和 Razor引擎冲突, 另外,当使用VS的格式化代码功能时, 会把 html ...

  5. poj 1905 Expanding Rods(木杆的膨胀)【数学计算+二分枚举】

                                                                                                         ...

  6. FreeBSD 安装过程

    FreeBSD安装步骤: 回车 按默认回车 输入服务器的计算机名 去掉games,加上src安装如下图 Lib32 ports src这三项一定要安装上 回车 选择Manual 进入以后点create ...

  7. tcp三次握手过程

    TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...

  8. struts2 if标签示例[转]

    下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...

  9. Eclipse常用快捷键(转帖)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...

  10. 正则表达式java,javaScript应用

    dfa nfa 混合:捕获:断言:  正则引擎大体上可分为不同的两类:DFA和NFA,而NFA又基本上可以分为传统型NFA和POSIX NFA.   1.正则语法 捕获组: 没用()的字符都是一个一个 ...