POJ 2955 Brackets (区间dp入门)
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 i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … 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 题意给你一个只含()[]的字符串,问你最多能配成对的有多少个和字符。
区间dp的入门题。整理下思路dp[i][j]表示区间i~j之间最大的匹配字符数。
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')) ————>dp[i][j]=dp[i+1][j-1]+2; 懂吧
代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream> using namespace std;
char s[];
int dp[][];
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%s",&s))
{
if (s[]=='e')
break ;
memset(dp,,sizeof dp);
int len=strlen(s);
for (int k=;k<len;++k)
{
for (int i=,j=k;j<len;++i,++j)
{
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[i][j]=dp[i+][j-]+;
for (int x=i;x<j;x++)
dp[i][j]=max(dp[i][j],dp[i][x]+dp[x+][j]);
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
POJ 2955 Brackets (区间dp入门)的更多相关文章
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- A - Brackets POJ - 2955 (区间DP模板题)
题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...
- POJ 2955 Brackets 区间合并
输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp ...
随机推荐
- Oracle 部门员工查询
--部门:部门编号,部门名称,地址: --员工:员工编号,员工名字,职务,管理编号,入职日期,薪资,奖金,部门编号: CREATE TABLE dept( deptno INT PRIMARY KEY ...
- shell脚本学习 (9) 提取开头或结尾的几行
1 提取开头的n行 用head awk或者 sed实现 do.txt sed 1q do.txt awk 'FNR <= 1' do.txt do.txt文件 2 显示行尾的几行 用tail - ...
- <三剑客> 老大:awk命令用法
awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一 个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是lin ...
- delphi 使用 InputBox、InputQuery 的启发
使用 InputBox.InputQuery 的启发 看了 InputBox.InputQuery 函数实现的源码, 有些收获与心得... ------------------------------ ...
- C++ string.replace的使用
//下面是一个检查一个字符串中是否有'.'的函数,该函数将找到的'.'转化为'_'. inline void checkName(string& name) { std::; while (s ...
- BUUCTF | MD5
题目:e00cf25ad42683b3df678c61f42c6bda flag{admin1} 算是一个资源收集吧,Orz,https://www.cmd5.com/
- python中的_ElementUnicodeResult是什么
_ElementUnicodeResult在python中是字符串的一种,因为在python3中,字符串就是指以unicode编码规则存储的数据,而以其他方式如utf-8,ASCII编码方式存储的数据 ...
- 全面了解python中的类,对象,方法,属性
全面了解python中的类,对象,方法,属性 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...
- SAS中的Order By - Proc Sort
SAS中的Order By - Proc Sort 1.排序proc sort proc sort在按数据集中某一个变量或几个变量的升序或降序将记录重新排列,并把结果保存在输出数据集中,如果不另外指定 ...
- upc组队赛14 As rich as Crassus【扩展中国剩余定理】
As rich as Crassus 题目链接 题目描述 Crassus, the richest man in the world, invested some of his money with ...