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

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

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

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<string>
  5. #include<cstring>
  6. #include<cmath>
  7. #include<set>
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11. typedef pair<double,double>ppd;
  12. const double PI = acos(-1.);
  13. const double eps = (1e-9);
  14. const int N=20;
  15. const int M=(1<<16);
  16. bool dp[N][M];
  17. bool ok[M];
  18. string s;
  19. char st[N];
  20. int main()
  21. {
  22. //freopen("data.in","r",stdin);
  23. int T;
  24. scanf("%d",&T);
  25. while(T--)
  26. {
  27. int n;
  28. cin>>s;
  29. n=s.length();
  30. int m=(1<<n);
  31. memset(ok,true,sizeof(ok));
  32. for(int i=1;i<m;++i)
  33. {
  34. int ln=0;
  35. for(int j=0;j<n;++j)
  36. {
  37. if((i&(1<<j)))
  38. st[ln++]=s[j];
  39. }
  40. for(int j=0;j<ln;++j)
  41. if(st[j]!=st[ln-1-j])
  42. {ok[i]=false;break;}
  43. }
  44. memset(dp,false,sizeof(dp));
  45. dp[0][m-1]=true;
  46. int ans=n;
  47. for(int i=0;i<n;++i)
  48. {
  49. for(int j=0;j<m;++j)
  50. if(dp[i][j])
  51. {
  52. if(ok[j])
  53. {dp[i+1][0]=true;break;}
  54.  
  55. for(int k=j;k>0;k=j&(k-1))
  56. if(ok[k])
  57. dp[i+1][j^k]=true;
  58.  
  59. }
  60. if(dp[i+1][0]==true)
  61. {ans=i+1;break;}
  62. }
  63. printf("%d\n",ans);
  64. }
  65. return 0;
  66. }

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. mysql5.1版本 my.cnf中复制的配置不起作用

    原因是:在mysql存放数据的目录下面有一个文件master.info,mysql服务器认为 master.info比my.cnf优先级要高,第一次启动slave,master.info不存在,从my ...

  2. gcc常用指令及相关知识

    1,gcc与g++的问题: 1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序:后缀为.cpp的,两者都会认为是c++程序. 2.编译阶段,g++会调用gcc,对于c++代码,两者是等 ...

  3. iOS 开发之 Xcode6 installation failed invalid argument!

    1.运行模拟器的时候 报出: installation failed invalid argument! 原因分析: 我把Bundle indentifier 置为空了! http://stackov ...

  4. Axure_元件库

    1.百度“推荐”,看到一篇文章“用Axure制作Material Design的APP原型(附元件库下载)” 想到 可以搜索 类似“axure 元件库”的关键字,来看看有哪些现成的元件库 2.

  5. python语法笔记(五)

    1.python内存管理 (1)对象内存使用 &nsbp;&nsbp;&nsbp;&nsbp;在python中通过执行内建函数 id(obj) 可以获得对象obj在内存 ...

  6. python操作mongodb之六自定义类型存储

    from pymongo.mongo_client import MongoClient client=MongoClient('192.168.30.252',27017) client=drop_ ...

  7. 线性表 - 从零开始实现by C++

    参考链接:数据结构探险之线性表篇     线性表

  8. javaweb2 URL(查找的过程)

    URL: 全名叫统一资源定位符,用于定位互联网的资源. 问题:接上(javaweb1 tomcat)http://localhost:8080/myweb/test.html 分析:http://-- ...

  9. java 导入包

    导入包 问题:类名冲突时,要如何解决. 解决:sun提供导入包语句让我们解决该问题. 导入包语句的作用:简化书写. 导入包语句的格式:import 包名.类名;(导入xxx包的XX类) 导入包语句的细 ...

  10. Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)

    http://www.xuanyusong.com/archives/3727 感谢楼下的牛逼回复更正一下,我表示我也是才知道.. 其实不需要实例化也能查找,你依然直接用GetComponentsIn ...