nyoj 括号匹配
1 | (1) | (3) | (6) | (10) |
1 | (2) | (5) | (9) | |
1 | (4) | (8) | ||
1 | (7) | |||
1 |
// ConsoleApplication8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;
#define min(x,y) (x < y ? x : y) bool isEqual(char c,char b)
{
if(c=='('&&b==')') return true;
if(c=='['&&b==']') return true;
return false; } int main()
{
int dp[][]; int len;
cin>>len;
while(len--)
{
string a;
cin>>a;
int len=a.length();
memset(dp,,sizeof(dp));//clear
dp[][]=;
for(int i=;i<len;i++)
{
dp[i][i]=; for(int j=i-;j>=;j--)
{
dp[j][i]=;
if(isEqual(a[j],a[i]))
{
dp[j][i]= min(dp[j][i],dp[j+][i-]);
}
for(int k=j;k<i;k++)
{
dp[j][i]=min(dp[j][k]+dp[k+][i],dp[j][i]); } } } cout<<dp[][len-]<<endl; } }
// ConsoleApplication8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;
#define min(x,y) (x < y ? x : y) bool isEqual(char c,char b)
{
if(c=='('&&b==')') return true;
if(c=='['&&b==']') return true;
return false; } int main()
{
int dp[][]; int len;
cin>>len;
while(len--)
{
string a;
cin>>a;
int len=a.length();
memset(dp,,sizeof(dp));//clear
// cout<<dp[45][56]<<endl;
dp[][]=;
dp[][]=;
for(int i=;i<=len;i++)
{
dp[i][i]=; for(int j=i-;j>=;j--)
{
dp[j][i]=dp[j][i-]+; //没有匹配的情况
for(int k=j;k<i;k++)
{
if(isEqual(a[k-],a[i-]))
{
dp[j][i]= min(dp[j][i],dp[j][k-]+dp[k+][i-]);
} } } } cout<<dp[][len]<<endl; } }
nyoj 括号匹配的更多相关文章
- [原]NYOJ 括号匹配系列2,5
本文出自:http://blog.csdn.net/svitter 括号匹配一:http://acm.nyist.net/JudgeOnline/problem.php?pid=2 括号匹配二:htt ...
- NYOJ - 括号匹配(二)(经典dp)
括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描写叙述 给你一个字符串,里面仅仅包括"(",")","[&quo ...
- NYOJ 题目15 括号匹配(二)(区间DP)
点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...
- [NYOJ 15] 括号匹配(二)
括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")","[&qu ...
- nyoj 15 括号匹配(2)
括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")","[" ...
- nyoj 15 括号匹配(二) (经典dp)
题目链接 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些 ...
- DP_括号匹配序列问题
括号匹配问题 简单括号匹配问题是给出字符串,判断字符串中的括号是否匹配,此类问题核心解决方案就是利用栈的后进先出的特性,从左到右依次遍历字符串,遇左括号进栈,遇右括号将其与栈顶元素配对,若能配对,则栈 ...
- 括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
- YTU 3003: 括号匹配(栈和队列)
3003: 括号匹配(栈和队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 2 [提交][状态][讨论版] 题目描述 假设一个表达式中只允许包含三种括号:圆括号&quo ...
随机推荐
- [CSS]visibility 属性
定义和用法 visibility 属性规定元素是否可见. 提示:即使不可见的元素也会占据页面上的空间.请使用 "display" 属性来创建不占据页面空间的不可见元素. 说明 这个 ...
- RepeatedDNASequences BestTime_to_Buy_and_SellStockIV
/** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.cnblogs.com/lkzf/ * @Time: 2015 ...
- CGAL 介绍
CGAL组织 内核 数值健壮 基础库 扩展性 2.4 命名约定 Naming In order to make it easier to remember what kind of entity a ...
- mysql 远程连接 2003 Can't connect to MySQL server (10060)
mysql server 端的端口被防火墙挡出,没有开放
- Python多线程学习笔记
Python中与多线程相关的模块有 thread, threading 和 Queue等,thread 和threading模块允许程序员创建和管理线程.thread模块提供了基本的线程和锁的支持,而 ...
- 八,WPF 命令
WPF命令模型 ICommand接口 WPF命令模型的核心是System.Windows.Input.ICommand接口,该接口定义了命令的工作原理,它包含了两个方法和一个事件: public in ...
- Kinetic使用注意点--canvas
<virtual> new Canvas(width, height) 参数: width:canvas宽度 height:canvas高度 方法: applyShadow(shape, ...
- BZOJ 2124等差子序列 线段树&&hash
[题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...
- MSCRM4.0如何使js事件在批量编辑表单中触发
MSCRM4.0如何使js事件在批量编辑表单中触发 MSCRM4.0如何使js事件在批量编辑表单中触发 MSCRM3.0我们可以通过在onload事件加入以下代码来控制某个属性为只读.crmForm. ...
- js去除重复数值
var c=[2,4,3,5,2,2,2], a = {}, i = 0; for(;i<c.length;i++){ a[c[i]] = 1 //利用对象名称不能重复的特性来去重 } c=[] ...