• <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 動態規劃

            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


                設dp[i,j]為從位置i到位置j需要加入字符的最小次數,有dp[i,j]=min(dp[i,k]+dp[k+1,j]),其中i<=k<j。特別的當s[i]='[' s[j]=']'或者s[i]='(' s[j]=')'時,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 極限定律 閱讀(2760) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2012年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久www免费人成精品| 久久99久久99小草精品免视看| 久久精品国产72国产精福利| 色偷偷91久久综合噜噜噜噜| 久久偷看各类wc女厕嘘嘘| 丁香久久婷婷国产午夜视频| 91麻豆国产精品91久久久| 久久久久亚洲AV无码网站| 亚洲国产日韩欧美久久| 国产精品久久久久久搜索| 麻豆精品久久久久久久99蜜桃| 久久精品国产久精国产| 欧美精品久久久久久久自慰| 国产香蕉97碰碰久久人人| 97精品依人久久久大香线蕉97 | 色综合久久中文综合网| 国产99久久久国产精品小说| 亚洲欧美日韩精品久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 精品多毛少妇人妻AV免费久久| 久久久久久精品成人免费图片 | 亚洲国产另类久久久精品| 久久青青草原精品国产软件| 久久精品九九亚洲精品天堂| 久久精品国产亚洲AV无码麻豆| 久久久无码精品亚洲日韩蜜臀浪潮| 久久综合视频网站| 久久婷婷五月综合97色直播| 中文精品久久久久国产网址 | 亚洲国产成人久久一区WWW| 国产精品免费久久久久电影网| 国产欧美一区二区久久| 99久久久国产精品免费无卡顿| 日日噜噜夜夜狠狠久久丁香五月| 囯产精品久久久久久久久蜜桃| 久久久久久亚洲精品影院| 国产精品久久久久蜜芽| 无码国产69精品久久久久网站| 亚洲国产精品无码久久| 韩国免费A级毛片久久| 国产成人久久精品区一区二区|