hdu4628(状态压缩DP)
来源:互联网 发布:linux怎么创建文本文件 编辑:程序博客网 时间:2024/06/06 19:13
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628
一道典型的状态压缩DP。。。也是想了好久又经过大神的提示才想出来的,马上要比赛了,然后又遇到点了情感方面的问题。。。真是作死的节奏。。。悲催的程序员命。。。不多说了,看题。。。
Pieces
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1532 Accepted Submission(s): 800
Problem Description
You heart broke into pieces.My string broke into pieces.But you will recover one day,and my string will never go back.
Given a string s.We can erase a subsequence of it if this subsequence is palindrome in one step. We should take as few steps as possible to erase the whole sequence.How many steps do we need?
For example, we can erase abcba from axbyczbea and get xyze in one step.
Given a string s.We can erase a subsequence of it if this subsequence is palindrome in one step. We should take as few steps as possible to erase the whole sequence.How many steps do we need?
For example, we can erase abcba from axbyczbea and get xyze in one step.
Input
The first line contains integer T,denote the number of the test cases. Then T lines follows,each line contains the string s (1<= length of s <= 16).
T<=10.
T<=10.
Output
For each test cases,print the answer in a line.
Sample Input
2aaabb
Sample Output
12
Source
2013 Multi-University Training Contest 3
Recommend
题解:
对一个长度为len 的字符串,用0和1来表示某个字符在子串中是否存在,则总共有 1<<len种情况,我们进行预处理,即针对每种情况算出它是否是回文的,然后用一个标记数组ok[i]来存下来,之后进行DP .
1.如何进行预处理,因为我们是用2进制来表示嘛,所以
bool check(int x){ if(x==0) return true; int i=0,j=len-1; while(i<j) { while(!(x&(1<<i))) i++; while(!(x&(1<<j))) j--; if(s[i]!=s[j]) return false; i++; j--; } return true;}
即 用 1<<i和1<<j的方式找到那个符合要求的非0位。
例如 x 011
i=0, 001
x&i!=0 即找到了最右边那位,这个大家得在纸上算一下,然后好好理解一下,要记住我们要找的是非0位。
2.接下来如何DP呢?
对于每种字串状态i,枚举包含状态i的状态j(既i中有1的位置j也有),然后判断状态j表示的字串消除的串i^j是否是回文串,是得话就可以从状态j到达状态i
对于如何枚举包含状态i的状态j:
for(int j=i;j<2^len;j=(j+1)|i);
比如:
i:1 1 0 1 0
j;1 1 0 1 0
则j+1:1 1 0 1然后(j+1)|i就将i中第一个为0的位置变为1
然后继续(j+1)|i其实就是在原前面已变位置的前提下,如果该位置前面还有0的就变成1,否则找下一个位置变为1
DP方程为 dp[i]=min(dp[i],dp[j]+1) //j为包含i的某个状态
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;const int MAXN=(1<<16)+10;const int INF=1<<30;char s[20];bool ok[MAXN];int dp[MAXN];int len,bit;bool check(int x){ if(x==0) return true; int i=0,j=len-1; while(i<j) { while(!(x&(1<<i))) i++; while(!(x&(1<<j))) j--; if(s[i]!=s[j]) return false; i++; j--; } return true;}int main(){ int T; while(cin>>T) { while(T--) { cin>>s; len=strlen(s); bit=1<<len; dp[bit-1]=0; // cout<<len<<" "<<bit<<endl; memset(ok,false,sizeof(ok)); for(int i=0;i< bit;i++) { ok[i]=check(i);//改成check[i]居然也能编译通过。。。。真怀疑我是不是眼睛花了。。。 } for(int i= bit-2;i>=0;i--) { dp[i]=INF; for(int j=i;j<bit;j=(j+1)|i) { if(!ok[i^j]) continue; dp[i]=min(dp[i],dp[j]+1); } } cout<<dp[0]<<endl; } } return 0;}
0 0
- hdu4628状态压缩DP
- HDU4628+状态压缩DP
- hdu4628(状态压缩DP)
- hdu4628(状态压缩dp)
- hdu4628之状态压缩dp
- hdu4628 状态压缩搜索or状态dp
- Pieces(hdu4628,状态压缩的动态规划)
- Pieces(hdu4628,状态压缩的动态规划)
- 状态压缩dp
- pku1038状态压缩dp
- 状态压缩DP 入门
- HDU1074 状态压缩DP
- Poj3254 状态压缩DP
- 状态压缩DP入门
- 【状态压缩DP】互不侵犯
- 【状态压缩DP】电子竞技
- HDU1074 状态压缩DP
- 【状态压缩DP】互不侵犯
- C# WinForm中LinkLabel的使用
- java并发编程实践学习(四)对象的发布和逸出之this逃逸
- 一个cocos2dx的扩展库
- Android客户端与服务端(jsp)之间json的传输与解析【附效果图附源码】
- Hbase总结
- hdu4628(状态压缩DP)
- Link - iOS Layout about "edgesForExtendedLayout" and "automaticallyAdjustsScrollViewInsets"
- 在Win8.1系统下如何安装运行SQL Server 2005 (以及安装SQL Server 2005 Express打补丁)
- 三正医疗概述
- c++构造函数和析构函数杂记
- 【原创】怎样查询自己上网的公网IP地址
- UIScrollView应用
- 底部操作栏的封装
- 联想G460安装黑苹果Mac OS X10.9.2之旅