• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            POJ 1141 Brackets Sequence 動(dòng)態(tài)規(guī)劃

            Description

            Let us define a regular brackets sequence in the following way:

            1. Empty sequence is a regular sequence.
            2. If S is a regular sequence, then (S) and [S] are both regular sequences.
            3. If A and B are regular sequences, then AB is a regular sequence.

            For example, all of the following sequences of characters are regular brackets sequences:

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

            And all of the following character sequences are not:

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

            Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.

            Input

            The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.

            Output

            Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

            Sample Input

            ([(]

            Sample Output

            ()[()]

            Source


                設(shè)dp[i,j]為從位置i到位置j需要加入字符的最小次數(shù),有dp[i,j]=min(dp[i,k]+dp[k+1,j]),其中i<=k<j。特別的當(dāng)s[i]='[' s[j]=']'或者s[i]='(' s[j]=')'時(shí),dp[i,j]=dp[i+1,j-1]。初始條件為dp[i,i]=1,其中0<=i<len。
            #include <iostream>
            using namespace std;

            const int MAXN = 110;
            char str[MAXN];
            int dp[MAXN][MAXN],path[MAXN][MAXN];

            void output(int i,int j){
                
            if(i>j) return;
                
            if(i==j){
                    
            if(str[i]=='[' || str[i]==']') printf("[]");
                    
            else printf("()");
                }

                
            else if(path[i][j]==-1){
                    printf(
            "%c",str[i]);
                    output(i
            +1,j-1);
                    printf(
            "%c",str[j]);
                }

                
            else{
                    output(i,path[i][j]);
                    output(path[i][j]
            +1,j);
                }

            }

            int main(){
                
            int i,j,k,r,n;
                
            while(gets(str)){
                    n
            =strlen(str);
                    
            if(n==0){
                        printf(
            "\n");
                        
            continue;
                    }

                    memset(dp,
            0,sizeof(dp));
                    
            for(i=0;i<n;i++) dp[i][i]=1;
                    
            for(r=1;r<n;r++)
                        
            for(i=0;i<n-r;i++){
                            j
            =i+r;
                            dp[i][j]
            =INT_MAX;
                            
            if((str[i]=='(' && str[j]==')')||(str[i]=='[' && str[j]==']'))
                                
            if(dp[i][j]>dp[i+1][j-1])
                                    dp[i][j]
            =dp[i+1][j-1],path[i][j]=-1;
                            
            for(k=i;k<j;k++)
                                
            if(dp[i][j]>dp[i][k]+dp[k+1][j])
                                    dp[i][j]
            =dp[i][k]+dp[k+1][j],path[i][j]=k;
                        }

                    output(
            0,n-1);
                    printf(
            "\n");
                }

                
            return 0;
            }

            posted on 2009-06-29 11:31 極限定律 閱讀(2744) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            麻豆久久| 国产三级久久久精品麻豆三级 | 久久精品国产亚洲77777| 欧美午夜A∨大片久久| 国产精品一区二区久久精品无码 | 久久99精品免费一区二区| 狠狠色噜噜狠狠狠狠狠色综合久久 | 国内精品久久久久影院优| 蜜臀久久99精品久久久久久小说 | 一本久久a久久精品亚洲| 精产国品久久一二三产区区别 | 久久精品国产亚洲精品2020| 浪潮AV色综合久久天堂| 亚洲中文久久精品无码| 人人狠狠综合久久88成人| 久久精品国产第一区二区三区| 久久精品一本到99热免费| 久久99国产精一区二区三区| 国内精品久久久久久久亚洲| 亚州日韩精品专区久久久| 久久精品卫校国产小美女| 精品人妻久久久久久888| 99久久国产综合精品成人影院| 久久www免费人成精品香蕉| 一级做a爰片久久毛片看看| 人妻精品久久久久中文字幕69| 亚洲伊人久久大香线蕉苏妲己| 亚洲国产成人久久综合区| 午夜精品久久久久久毛片| 香蕉久久夜色精品国产小说| 午夜精品久久久内射近拍高清| 欧美亚洲色综久久精品国产| 精品熟女少妇aⅴ免费久久| 国产亚洲精品久久久久秋霞| 日本精品久久久中文字幕| 久久婷婷五月综合国产尤物app| 国产精品一区二区久久不卡| 亚洲日本久久久午夜精品| 色综合久久最新中文字幕| 一本久道久久综合狠狠爱| 欧美激情精品久久久久久久九九九|