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),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- 在swift中使用oc 的代码
就是需要一个桥文件, 方法一:在swift项目中,新建一个oc的类,这时候,会弹出一个对话框,你点默认的那个选项就行了.然后在新生成的桥文件中导入你所需要的oc代码的头文件就行了. 方法二:但是有时候 ...
- Bootstrap 标签的变体 实例样式
Bootstrap 标签样式,代码如下: <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 标签的 ...
- GTEST-ASSERT出错
ASSERT_TRUE(1==1); ASSERT_EQ(1,1); 会给出以下错误: "cfunctest_normal.cpp", line 121.9: 1540-0258 ...
- 基数排序(RadixSort)
1 基数排序的特点是研究多个关键字key,且多个key之间有权重之分, 或者可把单个key建模为含有多个key的排序 而计数排序.桶排序始终只有个一个key,或者说围绕着一个比较规则 Ex:比较 ...
- iOS进阶——App生命周期
State Description Not running The app has not been launched or was running but was terminated by the ...
- MFC中快速应用OpenCV(转)
转载链接:http://wiki.opencv.org.cn/index.php/MFC%E4%B8%AD%E5%BF%AB%E9%80%9F%E5%BA%94%E7%94%A8OpenCV 简介和缘 ...
- Android 控件收集
SwipeMenuExpandableListView https://github.com/tycallen/SwipeMenu-Expandable-ListView
- [大牛翻译系列]Hadoop系列性能部分完结
Hadoop系列性能部分完结.其它的部分发布时间待定. Hadoop系列将不再一日一篇,开始不定期发布.
- laravel扩展Debugbar
github地址:https://github.com/barryvdh/laravel-debugbar
- python 模拟ajax查询社工库...
在windows中使用,输入有关信息查询社工库,本来是网页版的,我把ajax请求提取出来.粗略的封装下,挺好玩. #coding:utf8 import urllib2,urllib from Bea ...