UVA1626 bracket sequence Brackets sequence (interval dp)

Topic Portal (Los Valley) title Portal (UVA)  


Problem-solving ideas

It is clear that a range of dp, of course, memories can search AC, said here about the range dp.

An important feature of the interval dp is the need to enumerate the intermediate node k

Look at this question, with f [i] [j] represents the number sequence i ... j formation of a legitimate need to add brackets,

Obviously, when s [i] == s [j], f [i] [j] = f [i + 1] [j-1], and then enumerate intermediate point k, dynamic transfer equations can write : f [i] [j] = max (f [i] [j], f [i] [k] + f [k + 1] [j])

In order to ensure the demand f [i] [j] when f [i + 1] [j-1], f [i] [k], f [k + 1] [j] has been completed request, a first layer i must enumerate backwards, the second layer being the j must enumerate (hand push to understand QAQ)

After obtaining f array, we must consider how to output, because the output is in the form of (S) or [S], so obviously recursive output, plus a few special if sentenced on OK.

However, this question after I finished writing the code has been wa, 30 minutes later found the problem.

When reading t, I started using cin >> t; after reading the solution to a problem, and finally found it should be cin >> t back together with getchar (); Why?

Finally found troubled me nearly an hour to the root of the problem -

Spicy chicken Luo Gu the wrong question! ! T between the sample in the first set of data and a row of spaces. . . (Original title reflects the importance of watching)

AC Code

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<set>
 9 #include<map>
10 #include<vector>
11 #include<iomanip>
12 #include<ctime>
13 #include<stack>
14 using namespace std;
15 string s;
16 int t,len,f[105][105];
17 void print(int l,int r){
18     if(l>r) return;
19     if(l==r){
20         if(s[l]=='('||s[l]==')') printf("()"); 
21         if(s[l]=='['||s[l]==']') printf("[]");
22         return;
23     }
24     if((f[l][r]==f[l+1][r-1])&&((s[l]=='('&&s[r]==')')||(s[l]=='['&&s[r]==']'))){
25         printf("%c",s[l]);
26         print(l+1,r-1);
27         printf("%c",s[r]);
28         return;
29     }
30     for(int k=l;k<r;k++) {
 31 is          IF (F [L] [R & lt] == F [L] [K] + F [K + . 1 ] [R & lt]) {
 32              Print (L, K);
 33 is              Print (K + . 1 , R & lt);
 34 is              return ; // to find a positive solution to the output and return
 35          }
 36      }
 37 [  }
 38 is  int main ()
 39  {
 40      Scanf ( " % D " , & T);
 41 is      getchar ();
 42 is      the while (T-- ) {
 43 is          Memset (F, 0x3F , the sizeof (F));
 44 is         getline(cin,s);
45         getline(cin,s);
46         len=s.length();
47         if(len==0){
48             printf("\n\n");
49             continue;
50         }
51         f[0][0]=0;
52         for(int i=1;i<len;i++) f[i][i]=1,f[i][i-1]=0;
53         for(int i=len-1;i>=0;i--){
54             for(int j=i+1;j<len;j++){
55                 if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')) f[i][j]=min(f[i][j],f[i+1][j-1]);
56                 for(int k=i;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
57             }
58         }
59         print(0,len-1);
60         printf("\n");
61         if(t) printf("\n");
62     }
63     return 0;
64 }

 

Guess you like

Origin www.cnblogs.com/yinyuqin/p/11605128.html