hdu 4628 - Pieces(压缩dp)
来源:互联网 发布:数据库入门基础知识 编辑:程序博客网 时间:2024/06/07 00:04
题意:
给定一个串,每次可以删除一个回文子串,问把全串删干净的最少次数。
思路:
用状态压缩把所有的状态枚举出来,用数组记录所表示状态所有字符被删除干净的最少次数。
状态:dp[x]表示在状态x下把所有字符删除的最少次数。
状态压缩:dp[x] = min{dp[x], dp[k]}(k是x的子集)
这样的dp的边界不再是dp[0]或dp[n]了。而是对每个状态都要设定原始值,如果某个状态是完全回文的,则dp[x] = 1,否则,dp[x] 就是一个最大值(目前子串的长度)
代码入下:
#include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define M 16#define INF 0x7fffffffchar s[25];int len, dp[1<<16];vector<int>q;int init(int x){ q.clear(); for(int i = 0; i <= len; ++i) if(x&(1<<i)) q.push_back(i); int k = q.size()-1; for(int i = 0; i <= k/2; ++i) if(s[q[i]]!=s[q[k-i]]) return q.size(); return 1;}int main (){ int t = 0; scanf("%d",&t); getchar(); while(t--) { gets(s); len = strlen(s); int all = 1<<len; all-=1; for(int s = 1; s <= all; ++s) { dp[s] = init(s); for(int s0 = s; s0; s0 = (s0-1)&s) dp[s] = min(dp[s], dp[s0]+dp[s0^s]); } printf("%d\n",dp[all]); } 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 集合DP
- [kmp+dp] hdu 4628 Pieces
- [状压dp] hdu 4628 Pieces
- HDU-4628 Pieces 状压DP
- HDU 4628 Pieces (状压DP)
- hdu 4628 Pieces(状压dp)
- hdu 4628 Pieces(状态压缩+记忆化搜索)
- HDU 4628 Pieces
- HDU 4628 Pieces
- OC学习之UITextView
- C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
- openssl库中安全哈希算法的使用
- android工程项目导入问题
- Eclipse maven构建springmvc项目
- hdu 4628 - Pieces(压缩dp)
- C4.5算法
- 多校第三场 Hdu 4627 最小公倍数问题
- 最大子序列和的求解(时间复杂度O(N))
- HDU 1158 Employment Planning
- 第三章C#变量和表达式
- 使用WDK编译环境编译应用程序build命令(对话框程序)
- bat中如何等某个程序启动后再执行其他程序
- PHP中从前端页面表单到数据库的通信