SCU 1069 POJ 2955 Brackets

区间DP

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

char s[1000];
int dp[105][105];

int main()
{
    int i,j,k;
    while(~scanf(" %s",s))
    {
        if(strcmp(s,"end")==0) break;
        int len=strlen(s);
        for(i=len-1; i>=0; i--) s[i+1]=s[i];
        memset(dp,0,sizeof(dp));

        for(i=1; i<=len; i++) //长度
        {
            for(j=1; j<=len; j++) //起点
            {
                int w=i+j-1;//终点
                if(w<=j||w>len) continue;
                if((s[j]==‘(‘&&s[w]==‘)‘)||(s[j]==‘[‘&&s[w]==‘]‘))
                    dp[j][w]=dp[j+1][w-1]+2;
                for(k=j; k<w; k++)
                    dp[j][w]=max(dp[j][w],dp[j][k]+dp[k+1][w]);
            }
        }
        printf("%d\n",dp[1][len]);
    }
    return 0;
}
时间: 2024-09-30 05:35:05

SCU 1069 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(计数问题吗呵呵)

我只能说这道题和上一道动态规划的问题真的是太像了,连方法也一模一样 确实,计数也需要存状态,计数也是需要动规的. 此时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)

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]