poj-2955-Brackets-区间DP

poj-2955-Brackets-区间DP

 

Brackets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9014 Accepted: 4829

Description

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

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6

Source

Stanford Local 2004

 

 

一开始使用code2,找到总的pair数量再*2,以为就解决了。但是wrong answer,不知道本code的方法错在哪里?

使用了区间DP,方法参考自: http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051402.html

 

2955Accepted472K47MSG++858B2017-09-16 11:12:08

 

#include <cstdio> 
#include <cstring>const int MAXN = 110; #define max(a, b) (a)>(b)?(a):(b) char ch[MAXN]; 
int dp[MAXN][MAXN]; int Solve(int l, int r){if(dp[l][r] != -1){return dp[l][r]; }if(l>=r){dp[l][r] = 0; return 0; }else if(r == l +1 ){if((ch[l]=='(' && ch[r]==')') ||  (ch[l]=='[' && ch[r]==']') ){dp[l][r] = 2; }else{dp[l][r] = 0; }return dp[l][r]; }dp[l][r] = Solve(l+1, r); for(int k=l+1; k<=r; ++k){if((ch[l]=='(' && ch[k]==')') || (ch[l]=='[' && ch[k]==']')  ){dp[l][r] = max(dp[l][r], 2+Solve(l+1, k-1) + Solve(k+1, r)); }}return dp[l][r]; 
}int main(){freopen("in.txt", "r", stdin); while(scanf("%s", ch) != EOF){getchar(); if(strcmp(ch, "end") == 0){break; }int len = strlen(ch); memset(dp, -1, sizeof(dp)); int ans = Solve(0, len-1); printf("%d\n", ans );}return 0; 
}

  

 

采用count的方式来计算 subsequence,不知道wrong answer的地方是啥?

 

#include <cstdio>  
#include <cstdlib>  #include <cstring> const int MAXN = 12; int main(){freopen("in.txt", "r", stdin);  int ans, f1,f2, len; char ch[MAXN]; while(scanf("%s", ch) != EOF){getchar(); if(strcmp(ch, "end") == 0){break; } len = strlen(ch); ans = 0;f1 = 0; f2 = 0;  for(int i=0; i<len; ++i){if(ch[i] == '('){++f1; }else if(ch[i] == '['){++f2; }else if(ch[i] == ')'){if(f1 > 0){++ans; --f1; }}else if(ch[i] == ']'){if(f2 > 0){++ans; --f2; }}}printf("%d\n", 2*ans );} return 0; 
} 

  

 

转载于:https://www.cnblogs.com/zhang-yd/p/7530650.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371197.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python调用(运行)外部程序

在Python中可以方便地使用os模块运行其他的脚本或者程序&#xff0c;这样就可以在脚本中直接使用其他脚本&#xff0c;或者程序提供的功能&#xff0c;而不必再次编写实现该功能的代码。为了更好地控制运行的进程&#xff0c;可以使用win32process模块中的函数。如果想进一步控…

Java中已检查和未检查的异常

Java有两种类型的异常-已检查和未检查。 简而言之&#xff0c;选中的是指开发人员可以从异常中合理恢复的情况&#xff0c;而未选中的异常是无法处理的编程错误。 本文介绍了何时使用哪种。 但这不是那么简单–受检查的异常使代码变得“丑陋”。 它们迫使开发人员编写try / cat…

CCF - 201403-3 - 命令行选项

问题描述 试题编号&#xff1a;201403-3试题名称&#xff1a;命令行选项时间限制&#xff1a;1.0s内存限制&#xff1a;256.0MB问题描述&#xff1a; 问题描述请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项。每个命令行由若干个字符串组成,它们之间恰好由一个空…

java 枚举 values_JAVA 枚举运用一 values方法

importjava.lang.reflect.Method;importjava.lang.reflect.Type;importjava.util.Set;import java.util.*;public classEnumJavaClass {public enumEnumClass{One("参数变量枚举一"),Two("参数变量枚举二"),Three("参数变量枚举三");privateStri…

telnet测试端口是否正常打开

点击计算机的开始菜单--》运行 &#xff0c;输入CMD命令&#xff0c;然后确定。打开cmd命令行。 输入telnet测试端口命令&#xff1a; telnet IP 端口 或者 telnet 域名 端口 回车 如果端口关闭或者无法连接&#xff0c;则显示不能打开到主机的链接&#xff0c;链接失败 端口…

Linux历史,安装,分区,版本

Linux 历史 1970年是 UNIX元年&#xff0c;这一年 Kenneth Lane Thompson 和 Dennis Ritchie 合作编写了UNIX系统。Stallman 发起了GNU 计划&#xff0c;他本人开发了Emacs, GCC, GDB.Minix&#xff1a;教学用的类UNIX系统&#xff0c;由于UNIX是收费的且价格昂贵&#xff0c;因…

放弃Eclipse Juno

在上一个博客中&#xff0c;我发布了有关Eclipse 4.2 Juno设置的信息。 万一我需要重新安装其他东西&#xff0c;也可以作为参考。 当时我没有谈论的是我与Juno共同遇到的问题。 我以为这是我自己的安装程序&#xff0c;很麻烦&#xff0c;但是此后并没有太大改善。 我遇到的主…

Java instead of 用法_我又不是你的谁--java instanceof操作符用法揭秘

背景故事《曾经最美》是朱铭捷演唱的一首歌曲&#xff0c;由陈佳明填词&#xff0c;叶良俊谱曲&#xff0c;是电视剧《水晶之恋》的主题曲。歌曲时长4分28秒。 歌曲歌词&#xff1a;看不穿你的眼睛藏有多少悲和喜像冰雪细腻又如此透明仿佛片刻就要老去整个城市的孤寂不止一个你…

3.26

http://codeforces.com/gym/101196/attachments A题 B题 题意&#xff1a;一群人玩桌上足球(>4人)&#xff0c;分成黑白两队&#xff0c;每队有进攻和防守两名玩家&#xff0c;如果有一方失败则失败方的防守坐到等候席的结尾、进攻被流放到防守区再上来一个人作为进攻方。而…

scala akka通信机制

https://www.2cto.com/kf/201701/587514.html转载于:https://www.cnblogs.com/rocky-AGE-24/p/7542874.html

JUnit通过失败测试案例

为什么要建立一种预期测试失败的机制&#xff1f; 有一段时间&#xff0c;人们会希望并期望JUnit Test案例失败。 尽管这种情况很少见&#xff0c;但确实发生了。 我需要检测JUnit测试何时失败&#xff0c;然后&#xff08;如果期望的话&#xff09;通过而不是失败。 具体情况是…

CentOS6.5安装MySQL5.7详细教程

CentOS6.5安装MySQL5.7详细教程 注&#xff1a;文中所写的安装过程均在CentOS6.5 x86下通过测试 主要参考博文&#xff1a; https://segmentfault.com/a/1190000003049498 http://www.th7.cn/db/mysql/201601/175073.shtml 1.检测系统是否已经安装过mysql或其依赖&#xff0c;若…

cmake 查看编译命令,以及在vscode中如何使用cmke

通过设置如下配置选项&#xff0c;可以生成compile_commands.json 文件&#xff0c;记录使用的编译命令 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)获得现有模块列表 cmake --help-module-list查看命令文档 cmake --help-command find_file查看模块的详细信息 cmake --help-mo…

php学习八:封装

一&#xff1a;在php中&#xff0c;用class关键字来创建一个类&#xff0c;即进行封装&#xff1b;在类里面有成员属性和方法行为组成&#xff1a; 1.成员属性:用关键字var来声明,可以给初始值也可以不给;现在var废弃&#xff0c;用public来声明&#xff0c;public为共有属性&a…

纯Java JavaFX 2.0菜单

在有关JavaFX的最新文章中 &#xff0c;我集中讨论了不使用JavaFX 1.x的JavaFXScript和不使用JavaFX 2.0的新FXML来使用JavaFX 2.0的新Java API 。 所有这些示例均已使用标准Java编译器进行了编译&#xff0c;并使用标准Java启动 器执行。 在本文中&#xff0c;我将继续演示使用…

设置QtreeWidget水平滚动条

转载请注明出处&#xff1a;http://www.cnblogs.com/dachen408/p/7552603.html //设置treewidget水平滚动条 ui.treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);ui.treeWidget->header()->setStretchLastSection(false);转载于:https…

java 序列化 uid,Java中的序列化版本uid

How is Serialization id stored in the instance of the object ?The Serialization id we declare in Java is static field;and static fields are not serialized.There should be some way to store the static final field then. How does java do it ?解决方案The ser…

HTML5本地存储

什么是Web Storage Web Storage是HTML5里面引入的一个类似于cookie的本地存储功能&#xff0c;可以用于客户端的本地存储&#xff0c;其相对于cookie来说有以下几点优势&#xff1a; 存储空间大&#xff1a;cookie只有4KB的存储空间&#xff0c;而Web Storage在官方建议中为每个…

番石榴秒表

番石榴的秒表是番石榴第10版的另一个新番石榴类&#xff08;作为Optional &#xff0c;这是另一篇近期文章的主题&#xff09;。 顾名思义&#xff0c;这个简单的类提供了一种方便地测量两个代码点之间经过的时间的方法。 与使用System.currentTimeMillis&#xff08;&#xff…

CF 839 E-最大团

CF 839 E Soltion: 就是怎么求最大团的问题: 以下是\(O(7000\times n^2)\)的做法 求一个最大团,然后将所有的药水平均分配,到最大团的所有点上,计算答案. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorit…