/*
状态压缩DP
dp[ i ]:达到i状态的最小step。
题意:每次可以去掉一个回文串,求最少几步能取完。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
typedef long long ll;
//typedef __int64 int64;
const int maxn = ;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const double eps = 1e-;
int dp[ <<maxn ];
char s[ maxn ];
int state[ <<maxn ];//回文的状态 bool JudgeOneZero( int ss,int len ){
int Index[ maxn ];
int cc = ;
int IndexOfString = ;
while( IndexOfString<len ){
if( ss%== ){// 所有的 “1” 代表该位置上有字母,即这些组合是回文串
Index[ cc++ ] = IndexOfString;
}
ss /= ;
IndexOfString++;
}
if( cc== ) return true;
int L,R;
L = ;
R = cc-;
while( L<=R ){
if( s[Index[L]]!=s[Index[R]] ) return false;
L++;
R--;
}
return true;
}//判断s是否是回文状态 int init_state( int len ){
int cnt = ;
int N = <<len;
state[ cnt++ ] = ;
for( int i=;i<N;i++ ){
if( JudgeOneZero( i,len )==true ){
state[ cnt++ ] = i;
}
}
return cnt;
} //初始化回文的状态 bool Judge( int cur,int nxt,int len ){//当前状态cur,前一状态nxt
int Index[ maxn ];
int cc = ;
int IndexOfString = ;
while( IndexOfString<len ){
if( cur%== ){
if( nxt%== ) return false;
}//当前状态为1,前一状态必须为1
if( nxt%== ){
if( cur%== ) return false;
}//前一状态是0,当前状态也必须是0
if( cur%==&&nxt%== ){
Index[ cc++ ] = IndexOfString;
}
IndexOfString++;
cur /= ;
nxt /= ;
}
if( cc== ) return true;
int L,R;
L = ;
R = cc-;
//printf("cc=%d\n",cc);
while( L<=R ){
if( s[Index[L]]!=s[Index[R]] ) return false;
L++;
R--;
}
return true;
} int main(){
int T;
scanf("%d",&T);
while( T-- ){
scanf("%s",s);
int n = strlen(s);
int cnt = init_state( n );
int N = (<<n);
for( int i=;i<N;i++ )
dp[ i ] = inf;
dp[ N- ] = ;
/*
for( int i=N-2;i>=0;i-- ){
for( int j=0;j<N;j++ ){
if( i==j ) continue;
if( Judge( i,j,n )==true ){
//printf("i=%d, j=%d\n",i,j);
dp[ i ] = min( dp[i],dp[j]+1 );
//printf("dp[%d] = %d\n\n",i,dp[i]);
}
}
}
*/
for( int i=N-;i>=;i-- ){
for( int j=;j<cnt;j++ ){
if( ==(i&state[j]) ){
dp[ i ] = min( dp[i],dp[state[j]|i]+ );
}
}
}
printf("%d\n",dp[]);
}
return ;
}

HDU4628+状态压缩DP的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. Memcached 安装配置

    安装: memcached -d install memcached -d start net start "Memcached Server" 卸载: memcached -d ...

  2. PSP个人软件开发工具需求分析文档

    第一部分:前景与范围 1.业务需求 1.1 背景 在目前的软件项目开发过程中,进度计划总是非常不准确,经常出现延期,而且大多数都无法给出一个相对比较准确的延迟时间.即使使用用例包.用例的方式组织需求, ...

  3. Swift类型检查与转换

    继承会发生在子类和父类中,如图所示,是一系列类的继承关系类图,Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类.这个继承关系类图的具体实现 ...

  4. 双网卡route配置

    目前仅适用于windows: 192.168.*.*网段适用于上外网的 10网段适用于内网 route add 10.0.0.0 mask 255.0.0.0 10.34.6.1route add 1 ...

  5. Android应用资源--之属性(Attribute)资源

    原文链接: http://wujiandong.iteye.com/blog/1184921 属性(Attribute)资源:属于整个Android应用资源的一部分.其实就是网上一堆介绍怎么给自定义V ...

  6. UML建模文章总结

    一.为什么要学习UML UML是Unified Modeling Language(统一建模语言)的简称.UML是对软件密集型系统中的制品进行可视化.详述.构造和文档化的语言.制品{Artifact} ...

  7. c#学习汇总-----------------多态

    刚开通了博客园的博客,原因是我的师兄和前辈们在这里写的一些学习笔记让我受益匪浅,所以决定从今天起用这个平台来记录我的点滴学习心得.我喜欢GIS二次开发,以后应该也不会脱离于此,对于编程我积累的知识太零 ...

  8. oracle 中proc和oci操作对缓存不同处理

    oracle 中proc和oci操作对缓存不同处理

  9. get post

    浅谈HTTP中Get与Post的区别 2009-03-31 14:51 by hyddd, 248341 阅读, 74 评论, 收藏, 编辑 Http定义了与服务器交互的不同方法,最基本的方法有4种, ...

  10. html5学习笔记——2016/4

    HTML5新增的结构元素: section     article     aside     header     hgroup     footer     nav     figure HTML ...