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. [LED]如何配置LCD背光和LED,调试方法

    [DESCRIPTION] 如何配置LCD背光和LED,调试方法 [SOLUTION]LCD背光和LED配置文件alps/custom/<proj name>lk/cust_leds.ca ...

  2. python paramiko模块SSH自动登录linux系统进行操作

    1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...

  3. 使用soapui调用webservice接口

    soapui是专门模拟调用webservice接口的工具,下面介绍下怎么使用: 1.下载soapui并安装: 2.以免费天气获取接口为例:http://www.webservicex.net/glob ...

  4. MySQL在windows系统中修改datadir路径后无法启动问题,报错1067

    windows server2008下如何更改MySQL数据库的目录的帖子已经很多了,这里简单介绍一个步骤,如果不成功请先查看其它帖子. 更改默认的mysql数据库目录 将 C:\Documents ...

  5. markdown 语法学习

    markdown是一种轻量级的标记语言,和html类似,但是易读性比html要好很多. 基本语法: 1.标题和正文分隔线 === //至少三个连续等号,最高阶标题 --- //或者三个连续的减号,第二 ...

  6. LinuxShell脚本攻略--第三章 以文件之名

    生成任意大小的文件文件权限.所有权和粘滞位创建不可修改文件生成空白文件查找符号链接及其指向目标head 与 tail只列出目录的其他方法在命令行中用 pushd 和 popd 快速定位(cd -)统计 ...

  7. html 实体 => htmlspecialchars

    因为没有办法判断用户输入的数据是否安全, 可能含有和sql语句相冲突的字符,比如 '  " < >,那就有一个办法, 把这些特殊字符处理一下,比如吧空格符号变成 ,这样sql语句 ...

  8. ActionErrors 使用说明 struts1 validate 处理流程 详细教程(转)

    转自(http://blog.csdn.net/wyx100/article/details/8736445). struts1  处理流程是  jsp  -->  ActionForm 中的A ...

  9. js实现页面跳转

    js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/java ...

  10. Shader中贴图知识汇总: 漫反射贴图、凹凸贴图、高光贴图、 AO贴图、环境贴图、 光照纹理及细节贴图

    原文过于冗余,精读后做了部分简化与测试实践,原文地址:http://www.j2megame.com/html/xwzx/ty/2571.html   http://www.cnblogs.com/z ...