zoukankan      html  css  js  c++  java
  • URAL 1183 Brackets Sequence

    URAL 1183

    思路:区间dp,打印路径,详见http://www.cnblogs.com/widsom/p/8321670.html

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define pb push_back
    #define mem(a,b) memset(a,b,sizeof(a))
    
    const int N=105;
    int dp[N][N];
    int pos[N][N];
    string s;
    void dfs(int l,int r){
        if(l>r)return ;
        if(l==r){
            if(s[l]=='('||s[l]==')')cout<<"()";
            else cout<<"[]";
        }
        else{
            if(pos[l][r]==-1){
                cout<<s[l];
                dfs(l+1,r-1);
                cout<<s[r];
            }
            else{
                dfs(l,pos[l][r]);
                dfs(pos[l][r]+1,r);
            }
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>s;
        for(int l=2;l<=s.size();l++){
            for(int i=0;i+l-1<s.size();i++){
                int j=i+l-1;
                if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')dp[i][j]=dp[i+1][j-1]+2,pos[i][j]=-1;
                for(int k=i;k<j;k++){
                    if(dp[i][k]+dp[k+1][j]>=dp[i][j]){
                        dp[i][j]=dp[i][k]+dp[k+1][j];
                        pos[i][j]=k;
                    }
                }
            }
        }
        dfs(0,s.size()-1);
        return 0;
    }
  • 相关阅读:
    expect
    grep
    Python函数
    Python的set
    Python字典
    Python循环
    Python条件判断
    Python列表
    Python字符串
    Python组织代码块的形式
  • 原文地址:https://www.cnblogs.com/widsom/p/8376072.html
Copyright © 2011-2022 走看看