• ACM POJ 2955


    A - Brackets

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Submit Status Practice POJ 2955

    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 regular brackets sequences, then ab is a regular brackets sequence.
    • no other sequence is a regular brackets sequence

    For instance, all of the following character sequences are regular brackets sequences:

    (), [], (()), ()[], ()[()]

    while the following character sequences are not:

    (, ], )(, ([)], ([(]

    Given a brackets sequence of characters a1a2an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < imn, ai1ai2 … aim is a regular brackets sequence.

    Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

    Input

    The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

    Output

    For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

    Sample Input

    ((()))
    ()()()
    ([]])
    )[)(
    ([][][)
    end

    Sample Output

    6
    6
    4
    0
    6
     
     
    开始没想,直接匹配,,样例是过了,不过还是果断挖了
    这是错误代码
    #include <iostream>
    #include <cstdio>
    #include <cstring>
     
     
    using namespace std;
    char bracket[105];
    bool visit[105];
    int ans;
     
    void Find(char c,int n,int l)
    {
        for(int i = n+1; i<l; i++)
        {
            if(bracket[i] == c)
            {
                visit[i] =true;
                ans +=2;
                return;
            }
        }
        return;
    }
     
    int main()
    {
     
        while(scanf("%s",&bracket))
        {
            ans = 0;
            if(bracket[0]=='e')
                return 0;
            memset(visit,0,sizeof(visit));
            int len = strlen(bracket);
     
            for(int i=0; i<len; i++ )
            {
                if(!visit[i])
                {
                    if(bracket[i] == '(')
                        Find(')',i,len);
     
                    if(bracket[i] == '[')
                        Find(']',i,len);
     
                }
     
     
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

      ac code

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    char bracket[105];
    int dp[105][105];
    int ans;
    
    
    int main()
    {
    
        while(scanf("%s",&bracket),bracket[0]!='e')
        {
            int i,j,k,n=strlen(bracket);
    
            for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                {
                    dp[i][j] = 0;
                }
    
            for(i=n-1; i>=0; i--)
                for(j=i+1; j<=n-1; j++)
                {
                    dp[i][j] = max(dp[i+1][j],dp[i][j-1]);
                    for(k=i+1; k<=j; k++)
                    {
                        if((bracket[i]=='('&&bracket[k]==')')||(bracket[i]=='['&&bracket[k]==']'))
                            dp[i][k]=max(dp[i][k],dp[i+1][k-1]+2);
    
                        dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
    
                    }
                }
            printf("%d
    ",dp[0][n-1]);
        }
    
        return 0;
    }
  • 相关阅读:
    Grunt学习笔记【7】---- grunt-contrib-less插件详解
    Grunt学习笔记【6】---- grunt-contrib-requirejs插件详解
    Grunt学习笔记【5】---- expand使用方法
    Grunt学习笔记【4】---- 通配符和模板
    Grunt学习笔记【3】---- filter使用方式和实现原理详解
    Grunt 学习笔记【2】---- 配置和创建任务
    用css去除chrome、safari等webikt内核浏览器对控件默认样式
    angular中通过$location获取路径(参数)的写法
    angular自身带有的操作cookie的方法
    js中apply方法的使用
  • 原文地址:https://www.cnblogs.com/wejex/p/3250011.html
Copyright © 2020-2023  润新知