hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

原题: BZOJ1032 (原题数据有问题)

/*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/
#include <bits/stdc++.h>
using namespace std;
int T, n, tot = 1, kase = 1;
char s[205];
int dp[202][202], cnt[205], col[205];
void solve() {
    memset(dp, 0x3f, sizeof(dp));
    tot = 1;
    n = strlen(s);
    cnt[tot] = 1, col[tot] = s[0] - '0';
    for (int i = 1; i < n; i++) {
        if (s[i] == s[i - 1]) cnt[tot]++;
        else {
            cnt[++tot] = 1;
            col[tot] = s[i] - '0';
        }
    }
    n = tot;
    for (int i = 1; i <= n; i++) dp[i][1] = cnt[i] >= 2 ? 1 : 2;
    for (int j = 2; j <= n; j++) {
        for (int i = 1; i + j - 1 <= n; i++) {
            if (col[i] == col[i + j - 1]) {
                if (cnt[i] + cnt[i + j - 1] < 4) {
                    dp[i][j] = dp[i + 1][j - 2] + (cnt[i] + cnt[i + j - 1] == 2);
                    for (int k = 2; k < j; k++) {
                        if (col[i + k - 1] == col[i] && cnt[i + k - 1] == 1) {
                            dp[i][j] = min(dp[i][j], dp[i + 1][k - 2] + dp[i + k][j - k - 1]);
                        }
                    }
                }
                else dp[i][j] = dp[i + 1][j - 2];
            }
            for (int k = 1; k < j; k++) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[i + k][j - k]);
            }
        }
    }
    printf("Case #%d: %d\n", kase++, dp[1][n]);
}
int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%s", s);
        solve();
    }
    return 0;
}
// 011011100000001010101110011000 ans=4
// 10110011001101  ans=3

 

posted @ 2017-09-19 15:37 UnderSilence 阅读(...) 评论(...) 编辑 收藏