Bnuoj 1260 brackets Sequence

Source: Internet
Author: User
Brackets sequencetime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 1141
64-bit integer Io format: % LLD Java class name: mainspecial judge 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. inputthe input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them. outputwrite 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
()[()]
Sourcenortheastern Europe 2001: This DP, I am a scum. Every time I do this, I feel new! The water is so deep! Note that this is a singleton! Changed to a variety of examples, WA now
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 110;18 int dp[maxn][maxn],c[maxn][maxn] = {-1};19 char str[maxn];20 void print(int i,int j) {21     if(i > j) return;22     if(i == j) {23         if(str[i] == ‘(‘ || str[j] == ‘)‘)24             printf("()");25         else printf("[]");26     } else {27         if(c[i][j] >= 0) {28             print(i,c[i][j]);29             print(c[i][j]+1,j);30         } else {31             if(str[i] == ‘(‘) {32                 printf("(");33                 print(i+1,j-1);34                 printf(")");35             } else {36                 printf("[");37                 print(i+1,j-1);38                 printf("]");39             }40         }41     }42 }43 void go() {44     int len = strlen(str),i,j,k,theMin,t;45     for(i = 0; i < len; i++) dp[i][i] = 1;46     for(k = 1; k < len; k++) {47         for(i = 0; i+k < len; i++) {48             j = i+k;49             theMin = dp[i][i]+dp[i+1][j];50             c[i][j] = i;51             for(t = i+1; t < j; t++) {52                 if(dp[i][t]+dp[t+1][j] < theMin) {53                     theMin = dp[i][t]+dp[t+1][j];54                     c[i][j] = t;55                 }56             }57             dp[i][j] = theMin;58             if(str[i] == ‘(‘ && str[j] == ‘)‘ || str[i] == ‘[‘ && str[j] == ‘]‘) {59                 if(dp[i+1][j-1] < theMin) {60                     dp[i][j] = dp[i+1][j-1];61                     c[i][j] = -1;62                 }63             }64         }65     }66     print(0,len-1);67 }68 int main() {69     scanf("%s",str);70     go();71     puts("");72     return 0;73 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.