/*
状态压缩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. Unity3D项目实战笔记(10):Unity3D编译IPA的PostEvents–节约时间利器

    最近,SDK支付等接入差不多了,就从Unity3D生成IPA (企业版License), 然,需要手动执行的PostEvents竟然多大10项+, 这些我默默的承受了1周时间,每次约浪费20分钟-额外 ...

  2. 生成 网站“面包屑” XML

    using System; using System.Collections.Generic; using System.IO; using System.Threading; using Syste ...

  3. Kernel Panic常见原因以及解决方法

    Technorati 标签: Kernel Panic 出现原因 1. Linux在中断处理程序中,它不处于任何一个进程上下文,如果使用可能睡眠的函数,则系统调度会被破坏,导致kernel panic ...

  4. java学习笔记_GUI(1)

    import javax.swing.*; public class Gui { public static void main(String[] args) { JFrame frame = new ...

  5. 删除vim-minimal导致sudo不可用

    Ok, if anyone ends up in a similar situation, you can use pkexec yum install sudo. pkexec will let y ...

  6. 纯JS焦点图特效(可一个页面多用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 【Winform】锐浪报表使用

    在发开报表时,使用了锐浪报表. 需要注意的一些细节: 1.给staticbox设置文本,通过GridppReport的变量,使用ControlByName获取到控件后进行设置文字 _report.Co ...

  8. Winform程序只允许运行一个程序实例

    /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { Application ...

  9. js 根据当前星期做跳转(代码段)

    var week = [6,0,1,2,3,4,5]; $('.HotShop_head .HotShop_tab:eq('+week[new Date().getDay()]+')').click( ...

  10. php数组去重实例及分析

    php数组去重实例及分析.  一维数组的重复项: 使用array_unique函数即可,使用实例 <?php    $aa=array("apple","banan ...