• HDU 4628 Pieces(状压DP)题解


    题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次

    思路:把所有回文找出来,然后状压DP

    代码:

    #include<set>
    #include<map>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include <iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 16 + 5;
    const int M = maxn * 30;
    const ull seed = 131;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e4 + 7;
    char s[maxn];
    int pa[1 << maxn];
    int dp[1 << maxn];
    int n, cnt;
    bool check(int st){
        char tmp[20];
        int len = 0;
        for(int i = 0; i < n; i++){
            if(st & (1 << i)){
                tmp[++len] = s[i];
            }
        }
        for(int i = 1; i <= len / 2; i++){
            if(tmp[i] != tmp[len - i + 1]) return false;
        }
        return true;
    }
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            scanf("%s", &s);
            n = strlen(s);
            cnt = 0;
            memset(dp, INF, sizeof(dp));
            for(int i = 1; i < (1 << n); i++){
                if(check(i)){
                    pa[cnt++] = i;
                    dp[i] = 1;
                }
            }
            for(int i = 0; i < (1 << n); i++){
                for(int j = 0; j < cnt; j++){
                    if(i & pa[j]) continue;
                    dp[i | pa[j]] = min(dp[i | pa[j]], dp[i] + 1);
                }
            }
            printf("%d
    ", dp[(1 << n) - 1]);
        }
        return 0;
    }
  • 相关阅读:
    文章参考
    选择标识符(identifier)
    linux常见命令2
    Django框架之MVT(1)
    Tornado入门二
    2.Flask-jinjia2模板
    JQuery扩展和事件
    JQuery文档操作
    Jquery学习
    Jquery属性操作(入门二)
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10962920.html
Copyright © 2020-2023  润新知