學習 區間消除類dp

來源:互聯網
上載者:User

這一類的問題總結起來就是可以消除掉一段區間,之後原來的區間的兩邊會合起來,產生一種新的結果。
解決的一般辦法是假設一段已經處理過了,並記錄到後面/前面的狀態裡面去。 poj 1390 Blocks Description

Some of you may have played a game called ‘Blocks’. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shown below:


Figure 1

If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a ‘box segment’. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.

Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will 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 an initial state of this game. Input

The first line contains the number of tests t(1<=t<=15). Each case contains two 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 are 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

經典例題。
先把可以合并的都合并起來,然後對合并完的每一個元素分別處理,分情況討論。
設 f[i][j][k] f[i][j][k]表示合并 [i,j] [i,j]這個區間,j與後面k個顏色相同的一起考慮的最大得分。
首先枚舉長度,枚舉區間,對於一個區間來說,考慮右端點的合并情況: 右端點自己與後面的k個合并, [i,j−1] [i,j-1]合并起來。 右端點與 [i,j−1] [i,j-1]區間內的一個與右端點顏色相同 l l一起合并, f[i][l][k+len[j]]+f[l+1][j−1][0] f[i][l][k+len[j]]+f[l+1][j-1][0]

然後就可以合并了。

#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: 字串遊戲 Description

BX進行中一個字串遊戲,他手上有一個字串L,以及其他一些字串的集合S,然後他可以進行以下操作:對於一個在集合S中的字串p,如果p在L中出現,BX就可以選擇是否將其刪除,如果刪除,則將刪除後L分裂成的左右兩部分合并。舉個例子, L=′abcdefg′,S={′de′} L='abcdefg' , S=\{'de'\},如果BX選擇將 ′de′ 'de'從L中刪去,則刪後的 L=′abcfg′ L='abcfg'。現在BX可以進行任意多次操作(刪的次數,順序都隨意),他想知道最後L串的最短長度是多少。 Input

輸入的第一行包含一個字串,表示L。第二行包含一個數字n,表示集合S中元素個數。以下n行,每行一個字串,表示S中的一個元素。輸入字串都只包含小寫字母。 Output

輸出一個整數,表示L的最短長度。 Sample Input

aaabccd
3
ac
abc
aaa Sample Output

2 HINT

aaabccd
aacd
ad
對於100%資料,滿足 |L|<151,|S|<31 |L|, S

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.