First of all, I just want to state that this problem is poisonous ... I read in a char is wrong, but instead of a string read into the right or can be defined char array open, the original 1 a problem simply tangled long time.
Portal: Zjnu
Test instructions
is for a sequence of components, add as few parentheses as possible to get a sequence of rules, and output the length of the sequence.
But I learned two ways to define the DP state:
1) define DP[I][J] as the minimum number of parentheses to add to the i~j. Here we record S as the beginning of a character, E is the end position of a character.
① (a[s]== ' (' &&a[e]== ') ') | | (a[s]== ' [' &&a[e]== '] '), Dp[s][e]=min (dp[s][e],dp[s+1][e-1]);
② (a[s]== ' (' &&a[e]!= ') ') | | (a[s]== ' [' &&a[e]!= '] '), Dp[s][e]=min (dp[s][e],dp[s][e-1]+1);
③ (a[e]== ') ' &&a[s]!= ' (') | | (a[e]== '] ' &&a[s]!= ' ['), Dp[s][e]=min (dp[s][e],dp[s+1][e]+1);
④ then when there are other numbers in the middle of the two numbers, then we use for, like a stone, and then go to update dp[s][e]
The last output is as long as the output dp[0][len-1]+len.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < cstring>using namespace std; #define INF 99999999#define MAXN 111int Main () {string A;cin>>a;int l=a.length (); int dp[111][111];for (int i=0;i<l;i++) dp[i][i]=1;for (int len=2;len<=l;len++) {for (int s=0;s<=l-len;s++) {int E=s+len-1;dp[s][e]=inf;<span style= "White-space:pre" ></span>//!!! if ((a[s]== ' (' &&a[e]== ') ') | | (a[s]== ' [' &&a[e]== ']) Dp[s][e]=min (dp[s][e],dp[s+1][e-1]); if (a[s]== ' (' &&a[e]!= ') ') | | (a[s]== ' [' &&a[e]!= ']) Dp[s][e]=min (dp[s][e],dp[s][e-1]+1); if ((a[e]== ') ' &&a[s]!= ' (') | | (a[e]== '] ' &&a[s]!= ' [')] dp[s][e]=min (dp[s][e],dp[s+1][e]+1); for (int k=s;k<e;k++) {dp[s][e]=min (dp[s][e ],dp[s][k]+dp[k+1][e]);}}} printf ("%d\n", Dp[0][l-1]+l);}
2) The second state is defined in a somewhat different way from the first.
Defines DP[I][J] as the shortest length of a canonical string within an interval of i~j.
Of course here we need to initialize, for different locations of DP, we need to do different calculations. (It is important to initialize here.)
When a[s]== ' (' &&a[e]== ') ', then dp[s][e]=dp[s+1][e-1]+2;
Otherwise, go for a springboard and update dp[s][e].
In fact, the main idea is to calculate the minimum length of each cell, and then to update the value of the large interval according to the inter-cell.
The final output of the direct is dp[0][len-1] is OK.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < cstring>using namespace std; #define MAXN 111#define inf 99999999int main () {string A;cin>>a;int dp[111][111]; int len=a.length (); for (int. i=0;i<len;i++) {for (int j=0;j<len;j++) {if (i==j) dp[i][j]=2;if (i>j) dp[i][j]=0; else if (i<j) Dp[i][j]=inf;}} for (int l=2;l<=len;l++) {for (int s=0;s<=len-l;s++) {int e=s+l-1;if (a[s]== ' (' &&a[e]== ') ') | | (a[s]== ' [' &&a[e]== ']) {if (l>2) Dp[s][e]=dp[s+1][e-1]+2;else dp[s][e]=2;} for (int k=s;k<e;k++) {dp[s][e]=min (dp[s][e],dp[s][k]+dp[k+1][e]);}}} printf ("%d\n", Dp[0][len-1]);}
Understand!!! Extrapolate!! Come on!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZJNU (1183)--Bracket sequence "basic algorithm? Dynamic planning"--advanced