• POJ 2955 Brackets (区间DP)


    题目大意:

    给你一个字符串,问其中匹配的括号有多少个?  
    下面是用记忆化搜索写的。
     
    
    
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    const LL INF = 0xfffffff;
    const LL maxn = 255;
    char str[maxn];
    int dp[maxn][maxn];
    bool OK(int L,int R)
    {
        if((str[L] == '[' && str[R] == ']') || (str[L] == '(' && str[R] == ')'))
            return 2;
        return 0;
    }
    int DFS(int L,int R)
    {
        if(dp[L][R] != -1)
            return dp[L][R];
        if(L == R+1)
            return OK(L,R) ;
        if(L >= R)
            return 0;
        dp[L][R] = DFS(L+1, R);
    
        for(int i=L+1; i<=R; i++)
        {
            if( OK(L,i) )
                dp[L][R] = max(dp[L][R], DFS(L+1,i-1) + DFS(i+1,R) + 2);
        }
        return dp[L][R];
    }
    
    int main()
    {
        while(cin >> str, strcmp(str, "end"))
        {
            memset(dp, -1, sizeof(dp));
            printf("%d
    ",DFS(0, strlen(str)-1) );
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    C库函数中字符串处理函数集合(转)
    浅谈C++底层机制
    vc2008快捷键
    Delphi格式输出的用法
    Devenv 命令行开关
    DX皮肤控制
    C#格式化
    linq查询DataView
    WCF中的session用法
    VS2012clickonce发布问题
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4836272.html
Copyright © 2020-2023  润新知