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

状态压缩DP 时间复杂度应该是 16*(2^32)

但是运行时要远小于这个数 所以加一定剪枝就可以过

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=20;
const int M=(1<<16);
bool dp[N][M];
bool ok[M];
string s;
char st[N];
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
cin>>s;
n=s.length();
int m=(1<<n);
memset(ok,true,sizeof(ok));
for(int i=1;i<m;++i)
{
int ln=0;
for(int j=0;j<n;++j)
{
if((i&(1<<j)))
st[ln++]=s[j];
}
for(int j=0;j<ln;++j)
if(st[j]!=st[ln-1-j])
{ok[i]=false;break;}
}
memset(dp,false,sizeof(dp));
dp[0][m-1]=true;
int ans=n;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
if(dp[i][j])
{
if(ok[j])
{dp[i+1][0]=true;break;} for(int k=j;k>0;k=j&(k-1))
if(ok[k])
dp[i+1][j^k]=true; }
if(dp[i+1][0]==true)
{ans=i+1;break;}
}
printf("%d\n",ans);
}
return 0;
}

hdu 4628 Pieces的更多相关文章

  1. HDU 4628 Pieces(DP + 状态压缩)

    Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...

  2. HDU 4628 Pieces(状态压缩+记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索  ...

  3. [kmp+dp] hdu 4628 Pieces

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...

  4. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  5. hdu 4628 Pieces(状态压缩+记忆化搜索)

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  6. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

  7. HDU 4628 Pieces(状压DP)题解

    题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #includ ...

  8. hdu 4628(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 思路:首先把所有的回文找出来,如果当前状态为回文,则dp[state]=1,否则dp[state ...

  9. hdu 4614 pieces 状态DP

    题意:给你一个长度小于等于16的字符串,每次可以删除一个回文传,问你最少删除干净的字数. 状态+dp dp[i] = min(dp[i],dp[j]+dp[j^i]);(j是i的字串): 连接:htt ...

随机推荐

  1. oracle linux 下卸载

    1. 关闭数据库 shutdown immeidate 2. 停止 Listener lsnrctl stop 3. 停止http服务(可选) service httpd stop 4. 用su或者重 ...

  2. Python学习笔记13—错误和异常

    常见的异常:

  3. nginx相关优化

    1.配置监控nginx状态信息 vim /usr/locale/nginx/conf/nginx.conf server { listen ; server_name 192.168.1.30; lo ...

  4. netbeans for php常用快捷键

    文本编辑 复制当前行到上一行/下一行ctl+shf+up 移动当前行到上一行/下一行shf+alt+left/right/down/up 删除当前行ctl+E 生成下一行shf+enter(回车) 大 ...

  5. double 逆序

    请设计一个函数,不许用到字符串函数,用数学运算,将double类型数据转换,例如123.456转换成654.321 int _tmain(int argc, _TCHAR* argv[]) { con ...

  6. Redhat 6.4_联网 yum 配置

    步骤简述----------------------------------------------------------------- 1. 准备软件包 下载第三步的软件包即可.由于是联网yum配 ...

  7. JAVA获取当前系统时间System.currentTimeMillis()

    System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...

  8. NumPy(Numeric Python)使用方法

    NumPy官网 参考:<Python数据分析基础教程:NumPy学习指南> 用Python做科学计算(好东西) NumPy是python的核心库,是python机器学习编程的最底层的库,不 ...

  9. [转]Android 5.0——Material Design详解(动画篇)

    Material Design:Google推出的一个全新的设计语言,它的特点就是拟物扁平化. Material Design包含了很多内容,今天跟大家分享一下Material新增的动画: 在Andr ...

  10. 解决xshell 中文乱码

    在xshell命令行里面 输入:  locale输出: LANG=zh_CN.UTF-8LC_CTYPE="zh_CN.UTF-8"LC_NUMERIC="zh_CN.U ...