Pieces
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 311 Accepted Submission(s): 172
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
2
aa
abb
Sample Output
1
2
Source
Recommend
zhuyuanchen520
题意:
给定一个串,每次可以删除一个回文子串,问把全串删干净的最少次数。
思路:
用状态压缩把所有的状态枚举出来,用数组记录所表示状态所有字符被删除干净的最少次数。
状态:dp[x]表示在状态x下把所有字符删除的最少次数。
状态压缩:dp[x] = min{dp[x], dp[k]}(k是x的子集)
这样的dp的边界不再是dp[0]或dp[n]了。而是对每个状态都要设定原始值,如果某个状态是完全回文的,则dp[x] = 1,否则,dp[x] 就是一个最大值(目前子串的长度)
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; const int N=16; const int INF=0x3f3f3f3f; char str[30]; int len,dp[1<<16]; vector<int> vt; int solve(int k){ vt.clear(); for(int i=0;i<=len;i++) if(k&(1<<i)) vt.push_back(i); int sz=vt.size()-1; for(int i=0;i<sz/2;i++) if(str[vt[i]]!=str[vt[sz-i]]) return vt.size(); return 1; } int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ scanf("%s",str); len=strlen(str); for(int i=1;i<=(1<<len)-1;i++){ dp[i]=solve(i); for(int j=i;j;j=(j-1)&i) dp[i]=min(dp[i],dp[j]+dp[j^i]); } printf("%d\n",dp[(1<<len)-1]); } return 0; }
相关推荐
HDU的一题........HDU DP动态规
动态规划DP题解 POJ HDU部分动态规划DP题解
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,...
HDU上DP大集合,里面包括题,题解,代码,对DP入门者很实用,对DP老手也是有很大的提高
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
lazycal的集训队报告:初探数位DP 以HDU 2089,HDU 3652, URAL 1057等题目为例,介绍了数位DP的算法
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu 1166线段树代码
hdu动态规划算法集锦