POJ 2955... dp

本文探讨了括号序列的计算方法,并将其应用于实际场景中。通过代码实现,展示了如何利用C++解决括号匹配问题,涉及字符串操作、动态规划等核心算法,旨在提升编程技能和理解算法在具体问题上的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

括号序列

做了好多了

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;

#define READ freopen("acm.in","r",stdin)
#define ll long long
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define MPI map<int,int>::iterator 
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAX 500000
#define ROOT 0,n-1,1
#define PB push_back
#define FOR(a,b,c) for(int a=b;a<c;a++)
#define MOD 706679
#define keyTree (ch[ ch[root][1] ][0])

char s[500];
int dp[300][300];

int main()
{
    READ;
    while(scanf("%s",s)&&s[0]!='e')
    {
        MS(dp,0);
        int len=strlen(s);
        for(int i=len;i>=0;i--)
            s[i]=s[i-1];
        for(int l=2;l<=len;l++)
        {
            for(int st=1;st<=len;st++)
            {
                int end=st+l-1;
                if(s[st]=='('&&s[end]==')'||s[st]=='['&&s[end]==']')
                    dp[l][st]=dp[l-2][st+1]+1;
                for(int k=1;k<l;k++)
                    dp[l][st]=max(dp[l][st],dp[k][st]+dp[l-k][st+k]);
            }
        }
        cout<<dp[len][1]*2<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值