zoukankan      html  css  js  c++  java
  • poj2955(区间dp)

    题目连接:http://poj.org/problem?id=2955

    题意:给一个由()[]四种字符任意排列组成的字符串,求最长合法的不连续字串的长度。

    分析:如果找到一对匹配的括号[xxx]oooo,就把区间分成两部分,一部分是xxx,一部分是ooo,然后以此递归直到区间长度为<=1. 

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 100000000
    #define inf 0x3f3f3f3f
    #define eps 1e-9
    #define N 100010
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    int dp[110][110];
    char str[110];
    bool judge(int i,int j)
    {
        return (str[i]=='('&&str[j]==')')||(str[i]=='['&&str[j]==']');
    }
    int dfs(int l,int r)
    {
        if(dp[l][r]!=-1)return dp[l][r];
        if(l>=r)return 0;
        int temp=dfs(l+1,r);
        for(int i=l+1;i<=r;i++)
        {
            if(judge(l,i))
                temp=max(temp,dfs(l+1,i-1)+dfs(i+1,r)+2);
        }
        return dp[l][r]=temp;
    }
    int main()
    {
        int n;
        while(scanf("%s",str+1)>0)
        {
            if(strcmp(str+1,"end")==0)break;
            FILL(dp,-1);n=strlen(str+1);
            printf("%d
    ",dfs(1,n));
        }
    }
    View Code
  • 相关阅读:
    属性选择器
    Map
    tomcat 启动失败
    find for /f 分割字符串 bat
    oracle 1day
    scott lock
    oracle oracle sqldeveloper 12505 创建连接失败
    oracle sql developer 启动java.exe设置错误
    servlet post response.sendRedirect 乱码

  • 原文地址:https://www.cnblogs.com/lienus/p/4266567.html
Copyright © 2011-2022 走看看