HDU 4628 Pieces

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4628

题意:给你一个字符串,每次可以删除一个回文子序列,问最少删除多少次才能删完

 

因为字符串不长,所以可以先处理出每个状态下是否是回文子序列,然后状压dp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
char s[20];
int dp[1<<16];
bool check[1<<16];
bool pd(int x,int n)
{
    char a[20];
    int len=0;
    for(int i=0;i<n;i++)
        if (x>>i&1)
            a[len++]=s[i];
    for(int i=0;i<len/2;i++)
        if (a[i]!=a[len-1-i])
            return false;
    return true;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int n=strlen(s);
        int tot=1<<n;
        for(int i=1;i<tot;i++)
            check[i]=pd(i,n);
        memset(dp,0x3f,sizeof(dp));
        dp[0]=0;
        for(int i=1;i<tot;i++)
            for(int j=i;j;j=(j-1)&i)
                if (check[j])
                    dp[i]=min(dp[i],dp[i^j]+1);
        printf("%d\n",dp[tot-1]);
    }
    return 0;
}

  

 

posted @ 2017-09-04 15:45  BK_201  阅读(123)  评论(0编辑  收藏  举报