This sort of problem is summed up to eliminate a period of time, then the original interval of the two sides together, resulting in a new result.
The general solution is to assume that a section has been processed and recorded in the back/front state. POJ 1390 Blocks Description
Some of played a game called ' Blocks '. There is n blocks in a row with each box having a color. Here are an example:gold, silver, silver, silver, silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture would be as shown below:
Figure 1
If some adjacent boxes is all of the that same color, and both the box to its left (if it exists) and its right (if it exists) a Re of some other color, we call it a ' box segment '. There is 4 box segments. That is:gold, silver, bronze, gold. There is 1, 4, 3, 1 box (es) in the segments respectively.
Every time, you can click a box and then the whole segment containing that box disappears. If that segment was composed of K boxes, you'll get k*k points. For example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.
Now let's look at the picture below:
Figure 2
The first one is OPTIMAL.
Find the highest score you can get, given a initial state of this game. Input
The first line contains the number of tests T (1<=T<=15). Each case contains the lines. The first line contains an integer n (1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers is in the range 1~n. Output
For each test case, print the case number and the highest possible score. Sample Input
2
9
1 2 2 2 2 3 3 3 1
1
1 Sample Output
Case 1:29
Case 2:1 Source
Liu Rujia@poj Solution
Classic examples.
Merge all the things that you can merge, and then deal with each of the merged elements separately, and discuss them in a separate situation.
Set F[i][j][k] F[i][j][k] represents the combination of [i,j] [i,j] This interval, j with the following K-color together with the maximum score considered.
First, enumeration length, enumeration interval, for a range, consider the merger of the right end point: the right endpoint itself and the subsequent k merge, [i,j−1] [i,j-1] merged together. The right endpoint is merged with the same color as the right endpoint in the [i,j−1] [i,j-1] zone, f[i][l][k+len[j]]+f[l+1][j−1][0] f[i][l][k+len[j]]+f[l+1][j-1][0]
Then you can merge them.
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
const int MAXN = 201;
int T, N, M, V[MAXN], F[MAXN][MAXN][MAXN];
int PRE[MAXN], PO[MAXN], LEN[MAXN], CNT, now;
int sqr (int x) {return x*x;} int main () {scanf ("%d", &t); for (int TT = 1; TT <= T;
TT + +) {scanf ("%d", &n);
memset (f, 0, sizeof F), memset (pre, 0, sizeof pre);
memset (PO, 0, sizeof PO), memset (len, 0, sizeof Len);
CNT = 0, now = 0;
for (int i = 1; I <= n; i + +) scanf ("%d", &v[i]);
V[++CNT] = v[1], len[cnt] + +;
for (int i = 2; I <= n; i + +) {if (v[cnt] = = V[i]) len[cnt] + +;
else v[++ cnt] = V[i], len[cnt] = 1; } m = n;
n = cnt;
for (int i = 1; I <= n; i + +) pre[i] = Po[v[i]], po[v[i]] = i;
for (int i = 1; I <= n; i + +) for (int j = 0; J <= N; j + +) F[i][i][j] = Sqr (LEN[I]+J); for (int i = 2; I <= N;
i + +) for (int j = 1; j+i-1 <= N; j + +) for (int k = 0; k <= m; k + +) {
int II = j, JJ = I+j-1;
F[II][JJ][K] = f[ii][jj-1][0] + sqr (len[jj]+k); for (int l = PRE[JJ]; l >= j; l = Pre[l]) f[ii][jj][k] = max (F[ii][jj][k], F[II][L][K+LEN[JJ]] +
F[l+1][jj-1][0]);
} printf ("Case%d:%d\n", TT, f[1][n][0]);
} return 0; }
Bzoj 2121: String Game
Description
BX is in the middle of a string game, he has a string of L, and some other strings of the collection s, and then he can do the following: For a string in the set S p, if p in L, BX can choose whether to delete it, if deleted, Then the left and right parts of the split after L are removed are merged. For example, l=′abcdefg′,s={′de′} l= ' ABCDEFG ', s=\{' de ' \}, if BX chooses to delete the ′de′ ' de ' from L, then the l=′abcfg′l= ' abcfg ' after the deletion. Now BX can do any number of operations (delete the Times, the order is arbitrary), he would like to know the last L string of the shortest length is how much. Input
The first line of the input contains a string that represents L. The second line contains a number n, which represents the number of elements in the collection S. The following n rows, one string per line, represent an element in S. The input string contains only lowercase letters. Output
Outputs an integer that represents the shortest length of L. Sample Input
Aaabccd
3
Ac
Abc
AAA Sample Output
2 HINT
Aaabccd
Aacd
Ad
For 100% data, meet | l|<151,| s|<31 | l|, S