POJ - 2955 Brackets (区间DP)
题目:
给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度。
思路:
区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解。还是做的少。
代码:
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 1000000000
#define FRE() freopen("in.txt","r",stdin) using namespace std;
const int maxn = ;
char str[maxn];
int dp[maxn][maxn]; int main()
{
//FRE();
while(gets(str))
{
if(strcmp(str,"end")==) { break; }
if(strcmp(str,"")==) {printf("0\n"); break;}
int length = strlen(str);
for(int i=; i<length; i++)
{
for(int j=; j<length; j++)
{ dp[i][j] = ; }
}
//printf("length: %d\n",length);
for(int len=; len<length; len++)//枚举区间的长度
{
for(int i=; i+len<length; i++)
{
int j = i+len;
if((str[i]=='(' && str[j]==')') || (str[i]=='['&&str[j]==']'))
{
dp[i][j] = max(dp[i+][j-]+,dp[i][j]);//当前这个区间是由哪个区间得来的
}
for(int k=i; k<=j; k++)//更新这个区间的最优解
{
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
// printf("%d\n",dp[1][2]);
// printf("%d\n",dp[3][4]);
// printf("%d\n",dp[1][4]);
// printf("%d\n",dp[1][5]);
printf("%d\n",dp[][length-]);
}
return ;
}
POJ - 2955 Brackets (区间DP)的更多相关文章
- 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 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- 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 ...
随机推荐
- 【黑金教程笔记之003】【建模篇】akuei2的Verilog hdl心路
Verilog hdl不是“编程”是“建模” Verilog hdl语言是一种富有“形状”的语言. 如果着手以“建模”去理解Verilog hdl语言,以“形状”去完成Verilog hdl语言的设计 ...
- Marching squares 算法 获取轮廓(contour tracing)
https://en.wikipedia.org/wiki/Marching_squares http://blog.csdn.net/coolingcoding/article/details/1 ...
- ural1076 Trash 垃圾
Description You were just hired as CEO of the local junkyard.One of your jobs is dealing with the in ...
- PHP 简单答题系统
--sample 1: <!DOCTYPE html><html><head> <title>登录</title> <style ty ...
- 使用 Suricata 进行入侵监控(一个简单小例子访问百度)
前期博客 基于CentOS6.5下Suricata(一款高性能的网络IDS.IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐) 1.自己编写一条规则,规则书写参考snort规则(suricata ...
- Hibernate配置(外部配置文件方式)
配置Hibernate有2种方式,本文讲的是通过外部配置文件配置的方式 Hibernate核心配置文件 <?xml version='1.0' encoding='UTF-8'?> < ...
- 外文翻译 《How we decide》赛场上的四分卫 第二节
本书导言翻译 本章第一节 "决定是如何做出来的",关于意识最神秘的问题之一.尽管我们时刻做着决定,但是我们没有感觉到大脑内部的一系列有关进程.NFL球探挑选候选球员的评分表中,决策 ...
- 2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
思路: 状压dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- [BZOJ1008][HNOI2008]越狱 组合数学
http://www.lydsy.com/JudgeOnline/problem.php?id=1008 正着直接算有点难,我们考虑反着来,用全集减补集. 总的方案数为$m^n$.第一个人有$m$种可 ...
- Oracle数据库的SQL语句之完整性约束——基础篇
SELECT * FROM tb_clazz;SELECT * FROM tb_student; INSERT INTO tb_clazz(code,NAME,bzr) VALUES('1401',' ...