1 solutions

  • 0
    @ 2025-11-5 17:26:05

    C++ :

    //AUTHOR::STDAFX
    //ALGORITHM::DP
     
    #define MAXN 350UL
    #define INF 0X3fffffff
     
    #include <cstdio>
    #include <cstring>
     
    using namespace std;
     
    bool check(char,char);
     
    char input[MAXN];
    int n,del,f[MAXN][MAXN],tp;
     
    int main(){
        //freopen("t.txt","r",stdin);
        //freopen("","w",stdout);
        scanf("%s",input);
        n=strlen(input);
        for(int i=0;i<n;i++){
            f[i][i]=1;
        }
        for(int i=2;i<=n;i++){
            for(int j=0;j<=n-i;j++){
                tp=j+i-1;
                if(check(input[j],input[tp])){
                    f[j][tp]=f[j+1][tp-1];
                }
                else{
                	f[j][tp]=INF;
    			}
                for(int k=j;k<tp;k++){
                    if(f[j][tp]>f[j][k]+f[k+1][tp]){
                        f[j][tp]=f[j][k]+f[k+1][tp];
                    }
                }
            }
        }
        printf("%d",f[0][n-1]);
        //fclose(stdin);
        //fclose(stdout);
        return 0;
    }
     
    bool check(char a,char b){
        if((a=='('&&b==')')||(a=='['&&b==']')){
            return true;
        }
        return false;
    }
    
    • 1

    Information

    ID
    18428
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By