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 ...
随机推荐
- mysql更改数据文件目录及my.ini位置| MySQL命令详解
需求:更改mysql数据数据文件目录及my.ini位置. 步骤: 1.查找my.ini位置,可通过windows服务所对应mysql启动项,查看其对应属性->可执行文件路径,获取my.ini路径 ...
- struct--file_operations
struct--file_operations----------------------------------------- struct file_operations是一个字符设备把驱动 ...
- 捕获ClientDataSet.ApplyUpdates和SocketConnection异常
核心提示:如何捕获ClientDataSet.ApplyUpdates的错误,不用ReconcileError... var cdsEmp:TClientDataSet; //保存 procedure ...
- check whether the crystal report runtime is exists 检查crystalreport运行时是否存在
1. Try Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportClass() Dim rptView As New Cry ...
- EL四大作用域 9个jsp对象有效范围 及 对应的类
java中request,session,application的作用范围 page,request,session,application四者的作用范围: page的作用范围是当前页面:对应El表达 ...
- Oracle删除所有表
Oracle 清理用户所有的表 PLSQL中执行语句 select 'drop '||object_type||' '||object_name||';' from user_objects; 结果集 ...
- 背景CSS
.op_weather4_twoicon_day:hover .op_weather4_twoicon_hover { background-image: -webkit-linear-gradien ...
- JniHelper 含安卓推送
using System; using System.Runtime.CompilerServices; using UnityEngine; internal static class JniHel ...
- 日志分析-Web
http://my.oschina.net/chenguang/blog/376267 http://my.oschina.net/chenguang/blog/371275 http://my.os ...
- codeforces Mafia
/* * Mafia.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每个人都想玩若干场,求至少需要玩几场才可以满足大家的需求. * ...