HDU 4628 Pieces(状态压缩dp)
来源:互联网 发布:mac pdg转pdf 编辑:程序博客网 时间:2024/06/16 04:56
点击打开链接
题目大意:
给一个字符串,每次可以删除一个可不连续回文子串,问最少删几次可以全部删完。
思路:
因为字符串长度最大16,所以可用二进制状态表示, 1表示选取这个字符,0不选,组成一个子串。
先预处理出所有状态,看这个状态是不是回文。
f[i]表示状态i最少几次可以全删完, 初始化f数组INF
f[i] = min{f[i], f[s]+1 } s是i的子集。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<queue>#include<map>using namespace std;typedef long long int64;const int INF = 0x3f3f3f3f;const int MAXN = (1<<16)+10;char str[20];bool check[MAXN];int f[MAXN];int maxSta;bool isPalind(int st, int len){ char sub[20]; int idx = 0; for(int i=0; i<len; ++i){ if( (st>>i)&1 ) sub[idx++] = str[i]; } for(int i=0; i<idx/2; ++i){ if(sub[i] != sub[idx-1-i]) return false; } return true;}int main(){ int T; scanf("%d", &T); check[0] = false; while(T--){ scanf("%s", str); int len = strlen(str); maxSta = (1<<len)-1; for(int i=1; i<=maxSta; ++i){ check[i] = isPalind(i, len); } memset(f, 0x7f, sizeof(f)); f[0] = 0; for(int i=1; i<=maxSta; ++i){ for(int s=i; s; s=(s-1)&i) if(check[s]) f[i] = min(f[i], f[i^s]+1); } printf("%d\n", f[maxSta]); } return 0;}
- hdu - 4628 - Pieces(状态压缩dp)
- HDU 4628 Pieces(状态压缩dp)
- hdu 4628 Pieces(状态压缩DP)
- hdu 4628 Pieces (状态压缩dp)
- hdu 4628 Pieces (状态压缩dp)
- hdu 4628 Pieces 状态压缩DP
- HDU 4628 Pieces(状态压缩DP)
- hdu 4628 Pieces (状态压缩+二进制枚举+dp)
- hdu 4628 - Pieces(压缩dp)
- hdu 4628 Pieces(状态压缩+记忆化搜索)
- Pieces (状态dp)
- HOJ 2662 Pieces Assignment(状态压缩DP)
- HOJ 2662 Pieces Assignment(状态压缩DP)
- hdu 4628 Pieces(状压dp)
- hdu 4628(状态压缩dp)
- hdu 4628 Pieces 集合DP
- [kmp+dp] hdu 4628 Pieces
- [状压dp] hdu 4628 Pieces
- 参数声明中的静态数组索引:一个不错且鲜为人知的C语言特性
- [面经] 访问C++虚函数表
- librdkafka 0.8 源码解析与bug分析
- USACO 2.1.1The Castle
- POJ3155--Hard Life
- HDU 4628 Pieces(状态压缩dp)
- IOS---------------------------开发常用代码
- CI框架学习3
- 备忘录:审批流程的安全检测
- UVALive 4329 Ping pong(解法二:树状数组)
- hdu-数列有序
- python33 通过 cx_Freeze-4.3.1 打包 exe
- dynamips相关经验
- 理解mysql锁(2)表级锁定