• <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 極限定律 閱讀(2740) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            狠狠狠色丁香婷婷综合久久俺| 久久免费99精品国产自在现线 | 爱做久久久久久| 久久精品国产一区| 精品久久久无码中文字幕| 亚洲国产天堂久久综合| 久久久久亚洲av无码专区喷水| 9191精品国产免费久久| 久久无码一区二区三区少妇| 久久精品国产亚洲AV无码麻豆| 久久香蕉一级毛片| 亚洲AV无码久久寂寞少妇| 99久久综合国产精品二区| 久久久久亚洲AV无码专区体验| 久久久久人妻精品一区三寸蜜桃| 久久综合亚洲欧美成人| 欧美性大战久久久久久| 精品综合久久久久久97超人| 狠狠色丁香婷婷久久综合| 国产成人精品久久| 久久国产色AV免费观看| 久久人人爽人人爽人人爽| 国内精品久久久久久麻豆 | 99久久国产免费福利| 麻豆成人久久精品二区三区免费| 久久精品国产精品亚洲下载| 成人国内精品久久久久影院| 少妇精品久久久一区二区三区| 久久久久久亚洲精品不卡| 久久精品国内一区二区三区| 久久久国产乱子伦精品作者| 欧美大香线蕉线伊人久久| 漂亮人妻被黑人久久精品| 99久久国产综合精品女同图片| 久久久久久精品成人免费图片 | 人妻无码久久一区二区三区免费| 伊人久久精品影院| 一本一本久久A久久综合精品| 一本久久综合亚洲鲁鲁五月天| 亚洲欧美国产精品专区久久| 伊人久久一区二区三区无码|