First of all, I just want to state that this problem is poisonous ... I was wrong to read with Char, but it was right to read in string or to open a larger array of defined char, and the original 1 A was a struggle for a while.
Transmission door: ZJNU
Meaning
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've learned two ways to define a DP state:
1) define DP[I][J] as the minimum number of parentheses to be added in the i~j. Here we record S as the beginning of a character, and E is the ending 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 two numbers, then we use for, like a stone merge, and then update dp[s][e]
The final output of the time as long as the output Dp[0][len-1]+len good.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm >
#include <cstring>
using namespace std;
#define INF 99999999
#define MAXN
int 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;//!!!
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 of the definition and the first kind of a little different.
Defines the shortest length of a string that conforms to the specification in the interval within the I~j dp[i][j].
Of course we need to initialize here, and we need to do different calculations for the DP at different locations. (It's important to initialize here)
When a[s]== ' (' &&a[e]== '), then dp[s][e]=dp[s+1][e-1]+2;
Otherwise, go for the springboard and update dp[s][e].
In fact, the main idea is to calculate the length of each cell between the shortest, and then according to the cell and then to update the value of the large range.
The final output of the direct is dp[0][len-1] on it.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm >
#include <cstring>
using namespace std;
#define MAXN
#define INF 99999999
int 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...