• <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 動態(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。特別的當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 極限定律 閱讀(2738) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

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

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            av午夜福利一片免费看久久| 人妻少妇久久中文字幕| 一本色道久久88加勒比—综合| 久久久精品人妻一区二区三区四 | 国产精品久久久久影院色| 欧美精品一本久久男人的天堂| 久久精品免费网站网| 综合久久一区二区三区| 99国产精品久久| 国产aⅴ激情无码久久| 成人精品一区二区久久| 久久人人爽人人爽人人爽| 91秦先生久久久久久久| 国产成人精品三上悠亚久久| 欧美久久一级内射wwwwww.| 久久精品中文闷骚内射| 久久久高清免费视频| 草草久久久无码国产专区| 久久综合九色综合网站| 伊人久久大香线蕉成人| 久久强奷乱码老熟女网站| 久久久国产精品网站| 久久久久亚洲av无码专区| 狠狠综合久久AV一区二区三区| 久久久久亚洲精品中文字幕| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品久久久久免费a∨| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品久久久久久久| A狠狠久久蜜臀婷色中文网| 蜜臀av性久久久久蜜臀aⅴ| 久久妇女高潮几次MBA| 久久亚洲精品无码aⅴ大香| 色综合久久夜色精品国产| 亚洲а∨天堂久久精品9966| 久久久无码精品午夜| 日韩中文久久| 久久无码专区国产精品发布| 漂亮人妻被中出中文字幕久久| 色妞色综合久久夜夜| 久久夜色精品国产噜噜麻豆|