1.题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.

 

class Solution {
    public:
        int minCut(string s) 
        {
            //得到字符串长度为N
            const int N = s.size();
            if(N<=1) return 0;
            
            int i,j;
            //用一个二维bool数组isPalin表示是否子串s[i..j]是回文串
            bool isPalin[N][N];
            
            //algorithm中的fill_n函数:从当前起点开始,将之后N*N个元素赋值为FALSE
            fill_n(&isPalin[0][0], N*N, false);
            
            //数组元素minCuts[i]保存子串s[0..i-1]的最小分割次数,minCuts[i]初始值为i-1(最大的分割次数),minCuts[0]初始值设为-1, which is needed in the case that s[0..i-1] is a palindrome. 
            int minCuts[N+1];
            for(i=0; i<=N; ++i) 
                minCuts[i] = i-1;
            
            for(j=1; j<N; ++j)
            {
                for(i=j; i>=0; --i)
                {
                    //如果子串s[i..j]是回文串,则数组元素minCuts[j+1]将更新为minCuts[j+1]和minCut[i]+1中的最小值
                    if( (s[i] == s[j])&&( (j-i<2)||isPalin[i+1][j-1] ) )
                    {
                        isPalin[i][j] = true;
                        minCuts[j+1] = min(minCuts[j+1], 1 + minCuts[i]);    
                    }
                }
            }
            return minCuts[N];
            
        }
    };

LeetCode 132 分割回文串 Palindrome Partitioning II C++动态规划解法

2. 01背包问题c++实现


/*任务:计算0-1背包问题的最大价值
Sample Input
10 4
2 1
3 3
4 5
7 9
Sample Output
12
0 1 0 1
*/
#include<stdio.h>
#include<string.h>
int c[20][1000];//c[k][y]为只允许装前k种物品,背包总重量不超过y的最大价值
int inumber[21][1000];//inumber[k][u]为只允许装前K种物品,背包总重量不超过y时得到最大价值时使用的背包的最大标号
int w[21],p[21];
int knapsack(int m,int n)
{
	int i,j;
    for(i=1;i<n+1;i++)
        scanf("%d%d",&w[i],&p[i]);
    memset(c,0,sizeof(c));
	memset(inumber,0,sizeof(inumber));
	for(j=1;j<m+1;j++){
		c[1][j]=j/w[1]*p[1];
	}
    for(i=1;i<n+1;i++){
		for(j=1;j<m+1;j++){
			if(j >= w[i]){
				if(p[i]+c[i-1][j-w[i]]>=c[i-1][j]){
					c[i][j]=p[i]+c[i-1][j-w[i]];
					inumber[i][j]=i;
				}	
                else{
					c[i][j]=c[i-1][j];
					inumber[i][j]=inumber[i-1][j]; 
				}
			}
			else{
				c[i][j]=c[i-1][j];
				inumber[i][j]=inumber[i-1][j]; 
			}
		}
	}
	return(c[n][m]);                     
}
 
void trackSolution(int m, int n){
	int x[21];
	int y = m;
	int j = n;
	memset(x, 0, sizeof(x));
	while(true){
		j = inumber[j][y];
		x[j] = 1;
		y = y - w[j];
		while(inumber[j][y] == j){
			y = y - w[j];
			x[j]++;
		}
		if(!inumber[j][y]) break;
	}
	printf("最大价值方案中各个物品的个数为(物品标号从1到n):");
	for(j = 1; j <= n; j++){
		printf("%d ", x[j]);
	}
	printf("\n");
}
int main()
{
	int m,n;
	while(scanf("%d%d",&m,&n)!=EOF){
		printf("最大价值为%d\n",knapsack(m,n));
		trackSolution(m, n);
	}
	return 0;
}

 

相关文章:

  • 2021-10-20
  • 2021-07-01
  • 2021-11-15
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-06-01
  • 2021-08-04
  • 2021-11-04
  • 2021-04-05
相关资源
相似解决方案