HDU4628+状态压缩DP
/*
状态压缩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的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- ubuntu 更新软件源
ubuntu 更新软件源 修改文件sources.list 位于/etc/apt/sources.list,并备份原文件为sources.list.bak deb http://mirrors.163 ...
- MySQL数据库的登陆
MySQL数据库的登陆 MySQL是一种C/S结构. C/S(Client/Server)客户端/服务器 MySQL的客户端: 1.cmd客户端 2.可视化图形界面 3.php代码 登陆: MySQL ...
- Excel常用函数
1.基本的算数函数 sum() average() 2.三角函数 sin() cos() 3.
- 2013山东省“浪潮杯”省赛 A.Rescue The Princess
A.Rescue The PrincessDescription Several days ago, a beast caught a beautiful princess and the princ ...
- C++与Lua交互(二)
上一篇我们搭建好了整个的项目环境,现在,我们一起探索一下如何将lua寄宿到C++中. 宿主的实现 我们在LuaWithCPPTest项目下,查看Source.cpp代码如下: #include < ...
- 类似QQ的应用毗邻(Pilin)即时聊天源码
这个应用是从安卓教程网分享过了的,个人觉得这个还是挺不错的,毗邻(Pilin)即时聊天应用源码,承诺的 基于xmpp openfire asmack 的即时聊天应用,继续完善,现在只完成了文字.表 ...
- CSS3单位
em 做前端的应该对em不陌生,不是什么罕见的单位,是相对单位,参考物是父元素的font-size,具有继承的特点.如果字体大小是16px(浏览器的默认值),那么 1em = 16px. 不过,这样使 ...
- windows github 搭建与使用
git/github使用以下是全部在命令行使用(windows/github) 注册账户以及创建仓库先在github建立账号和创建仓库,此时为空的仓库 配置git下载并安装 git windows版本 ...
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.
一:使用jsonp格式, 如jquery中ajax请求参数 dataType:'JSONP'. <html> <head> <title>title</t ...
- js判断屏幕分辨率的代码
通过下面的代码判断分辨率 <script language="JavaScript"> <!-- Begin function redirectPage() { ...