HDU-4628 Pieces 如压力DP

鉴于他的字符串,每一个都能够删除回文子串。子可以是不连续,因此,像更好的模拟压力。求删除整个字符串需要的步骤的最小数量。

最大长度为16,因此不能逐行枚举状态。首先预处理出来全部的的回文子串,然后从第一步開始,依次状压第i步能到达的状态。假设能达到母串,跳出。

还有初始化不要用图省事用memset。

不优越的姿势+函数导致T了数发。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
char s[20];
char s1[20];
int ans;
int num;
int dp[1<<17][17];
int visit[20];
int Pow[15];
int a[1<<17];
bool solve(char str[])
{
	int flag=1;
	int start=0;
	int end=strlen(str)-1;
	while(start<=end)
	{
		if(str[start]!=str[end])
		{
			flag=0;
			break;
		}
		start++;
		end--;
	}
	if(flag)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	int t;
	int sum;
	Pow[0]=1;
	for(int i=1;i<=18;i++)
	{
		Pow[i]=Pow[i-1]*2;
	}
	scanf("%d",&t);
	while(t--)
	{
		num=0;
		scanf("%s",s);
		int len=strlen(s);
		int mos=1<<(len);
		for(int i=1;i<=len;i++)
		{
			for(int j=1;j<mos;j++)
			{
				dp[j][i]=-1;
			}
		}
		int len1;
		s1[0] = '\0';
		len1=0;
		int p;
		for(int i=1;i<mos;i++)
		{
			s1[0] = '\0';
			len1=0;
			for(int j=0;j<len;j++)
			{
				if(i&Pow[j])
				{
					s1[len1++]=s[j];
				}
			}
			s1[len1]='\0';
			//cout<<s1<<endl;
			if(solve(s1))
			{
				a[num++]=i;
			}
		}
		for(int i=1;i<=17;i++)
		{
			for(int j=0;j<num;j++)
			{
				if(i==1)
				{
					dp[a[j]][i]=1;
				}
				else
				{
					for(int k=1;k<mos;k++)
					{
						if(dp[k][i-1]!=-1)
						{
							if(a[j]&k)
							{
								continue;
							}
							dp[(k|a[j])][i]=1;
						}
					}
				}
			}
			if(dp[mos-1][i]!=-1)
			{
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-11-06 07:15:27

HDU-4628 Pieces 如压力DP的相关文章

HDU 4628 Pieces(状态压缩DP)

题目链接:传送门 题意: 给定一个长度小于16的字符串然后每次可以去掉它的一个回文子序列,问最少删除多少次可以使这个字符串为空. 分析: 首先预处理出这个字符串的所有回文子序列,然后将其压缩成二进制x,然后dp[x]表示这个序列删除所需要的最小的步数,然后dp[x] = min(dp[x],dp[sub])sub表示x的所有子序列. 代码如下: #include <iostream> #include <string> #include <cstring> #inclu

HDU -4628 Pieces

http://acm.hdu.edu.cn/showproblem.php?pid=4628 Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1610    Accepted Submission(s): 850 Problem Description You heart broke into pieces.My str

[状压dp] hdu 4628 Pieces

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4628 Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1665    Accepted Submission(s): 862 Problem Description You heart broke into pieces

[kmp+dp] hdu 4628 Pieces

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2096    Accepted Submission(s): 715 Problem Description Now you are back,and

HDU 4628 Pieces(状压DP)题解

题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm&

hdu 4628 Pieces(状态压缩+记忆化搜索)

Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1811    Accepted Submission(s): 932 Problem Description You heart broke into pieces.My string broke into pieces.But you will recover one

hdu 4960 Another OCD Patient(dp)

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 645    Accepted Submission(s): 238 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This mornin

HDU 1114 Piggy-Bank(完全背包 DP)

题意  知道空存钱罐的重量和装满钱的存钱罐的重量及每种币值的重量   求存钱罐里至少有多少钱 裸的完全背包  但是是求最小值  所以初始0要变成初始INF  max也要变成min #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10005, INF = 0x3f3f3f3f; int val[N], wei[N], d[N]; int ma

hdu 4960 记忆化搜索 DP

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 490    Accepted Submission(s): 180 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morni