POJ 2955 Brackets(计数问题吗呵呵)

我只能说这道题和上一道动态规划的问题真的是太像了,连方法也一模一样

确实,计数也需要存状态,计数也是需要动规的。

此时d【i】【j】表示的状态是s【i~j】的序列中有多少 不规则 的括号。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n;
char s[105];
int d[105][105];

bool match(char ch1,char ch2)
{
    if((ch1=='['&&ch2==']')||(ch1=='(')&&ch2==')')
        return true;
    return false;
}

void dp()
{
    for(int i=0;i<n;i++)
    {
        d[i+1][i]=0;
        d[i][i]=1;
    }
    for(int i=n-2;i>=0;i--)
    {
        for(int j=i+1;j<n;j++)
        {
            d[i][j]=n;
            if(match(s[i],s[j])) d[i][j]=min(d[i][j],d[i+1][j-1]);
            for(int k=i;k<j;k++)
                d[i][j]=min(d[i][j],d[i][k]+d[k+1][j]);
        }
    }
}

int main()
{
    while(scanf("%s",s))
    {
        if(strcmp(s,"end")==0) break;
        memset(d,0,sizeof(s));
        n = strlen(s);
        dp();
        printf("%d\n",n-d[0][n-1]);
    }
    return 0;
}

然后用总的减去不规则的,就得到了最多的规则括号数

POJ 2955 Brackets(计数问题吗呵呵)

时间: 2024-08-26 00:28:18

POJ 2955 Brackets(计数问题吗呵呵)的相关文章

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

poj 2955 Brackets dp简单题

//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int n; int rec(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return dp[l][r]=0; if(l+1==r){ if(s[l]=='('&&s[r]==')') return dp[l][r]=2; if(

POJ 2955 Brackets (区间dp 括号匹配)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3951   Accepted: 2078 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

poj 2955 Brackets【区间DP】

题目链接:http://poj.org/problem?id=2955 题意:求回文子串的最大长度. 解法:枚举区间长度,更新答案. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include

POJ 2955 Brackets

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6622   Accepted: 3558 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

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

POJ 2955 Brackets (区间DP)

题意:给定一个序列,问你最多有多少个合法的括号. 析:区间DP,dp[i][j] 表示在 第 i 到 第 j 区间内最多有多少个合法的括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios

poj(2955)——Brackets(区间dp)

题意: 现在我们定义一种R串,它必须满足以下条件: 1)当它的字串是空的时候时,那么它是R串. 2)当它是R串时,那么(s)或是[s]也是R串. 3)当a和b都是R串时,那么ab也是R串. 这里我没有完全领悟题目的意思,所以我发现递推不过去.其实它的实质就是括号匹配. 也就是说这里的合法序列是指括号能够两两匹配的. if((a[s]=='('&&a[e]==')')||(a[s]=='['&&a[e]==']')) 这里的这种情况时当外围是相互匹配的时候. dp[s][e]

poj 2955 Brackets(区间DP求最长匹配子串)

思路:假设要求区间[i,j]的最长匹配字串,它必然可以从[i,j-1]转移而来,有可能是s[j]与s[i]发生"关系"(匹配或不匹配),一直到s[j-1],若不发生"关系",即s[j]跟自己发生"关系",用for循环枚举所有的可能,取最大值. 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char