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. [转载] 理解 rto

    原文: http://mp.weixin.qq.com/s?__biz=MzAxNjM3MDkyOQ==&mid=204656491&idx=1&sn=5046aa16eee0 ...

  2. Java数组实现五子棋功能

    package ch4; import java.io.*; /** * Created by Jiqing on 2016/11/9. */ public class Gobang { // 定义棋 ...

  3. python中self,cls

    cls主要用在类方法定义,而self则是实例方法. self, cls 不是关键字,完全可以使用自己写的任意变量代替实现一样的效果. 普通的实例方法,第一个参数需要是self,它表示一个具体的实例本身 ...

  4. Hibernate Projections(投影、统计、不重复结果)

    Hibernate除了处理查询结果集中的对象之外,还可以将结果集中的结果当做行和列集来使用,这与通过JDBC执行select查询获得的数据的使用方式相似.因此,Hibernate也支持属性.统计函数和 ...

  5. spring中文乱码问题

    第一:code @RequestMapping(value = "/query/{keyword}", method = RequestMethod.GET, produces = ...

  6. OpenGL的glPushMatrix和glPopMatrix矩阵栈顶操作函数详解

    OpenGL中图形绘制后,往往需要一系列的变换来达到用户的目的,而这种变换实现的原理是又通过矩阵进行操作的.opengl中的变换一般包括视图变换.模型变换.投影变换等,在每次变换后,opengl将会呈 ...

  7. Oracle 修改一行数据内存主要变化

    向Oracle 数据库发出请求,修改一行数据,在内存中主要有以下变化: 1. 服务器进程将包含该行数据的块读取到内存中 2. 写redo日志.将内存中该数据块指向undo表空间中数据块的变更向量(Ch ...

  8. 自我总结(五)---(学习j2ee)

    自我完善的过程就是在不断的自我总结不断的改进. 我们这半个月来都是学习j2ee.这个知识是很重要的,一般我们出去工作都是会用到的.我们的星期六,星期天也是不上课的.所以说我相当于上了十天的j2ee了. ...

  9. 搭建一个简单的Struts2框架

    1  创建一个web项目. 2 导入必要的JAR文件. 放在WEB-INF下lib包里. 3 添加web.xml配置,添加启动配置. <?xml version="1.0" ...

  10. --tags --follow-tags 的区别

    --tags    All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the comm ...