• <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久久国产亚洲综合精品| 国内精品久久久久| 亚洲色欲久久久久综合网| 久久久久亚洲AV综合波多野结衣| 国产精品成人无码久久久久久 | 2021国产精品午夜久久| 国产综合久久久久久鬼色| 久久久久亚洲av成人无码电影 | 久久91精品国产91久| 国产精品一区二区久久国产| 久久免费高清视频| 99精品国产免费久久久久久下载| 日韩精品国产自在久久现线拍 | 国产91色综合久久免费| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 狠狠色婷婷久久一区二区| 91精品婷婷国产综合久久| 少妇熟女久久综合网色欲| 国产成人久久777777| 久久ZYZ资源站无码中文动漫| 国产女人aaa级久久久级| 996久久国产精品线观看| 精品久久久久久无码不卡| 99久久亚洲综合精品成人| 精品久久人妻av中文字幕| 亚洲午夜久久久影院伊人| 亚洲国产成人精品女人久久久 | 久久久久久毛片免费播放| 一本色综合久久| 国产精品无码久久久久| 精品久久久久久国产| 国产精品无码久久久久久| 热re99久久6国产精品免费| 亚洲欧美日韩中文久久| 亚洲精品国精品久久99热| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久久久香蕉视频| 久久精品国产免费观看| 欧美精品乱码99久久蜜桃| 国产精品久久久天天影视| 精品久久久久久久久午夜福利|