动态规划的一些程序

1、最长公共子串

public class sf_dp_LongestCommenSubString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	String str1="abcdepoiopswegsdjgnegh";  
        String str2="bcdefpoiqwnsuiguwnenwgenwuengiewugnwebignb";  
        int max = 0;
        int[][] dp = new int[str1.length()+1][str2.length()+1];
        for(int i=1;i<dp.length;i++){
        	char c1 = str1.charAt(i-1);
        	for(int j=1;j<dp[0].length;j++){
        		char c2 = str2.charAt(j-1);
        		if(c1==c2){
        			dp[i][j] = dp[i-1][j-1]+1;
        		}else{
        			dp[i][j] = 0;
        		}
        		max = Math.max(max, dp[i][j]);
        	}
        }
        System.out.println(max);
	}

}

2、最长递增子序列

public class sf_dp_LIS {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] nums = {3,5,6,2,5,4,19,5,6,8,12};
		int len = 1;
		int[] max = new int[nums.length];
		int[] maxindex = new int[nums.length];
		max[0] = nums[0];
		maxindex[0] = 0;
		for(int i=1;i<nums.length;i++){
			if(nums[i] > max[len-1]){
				max[len] = nums[i];
				maxindex[len] = i;
				len++;
			}else{
				int pos = binarysearch(max,0,len-1,nums[i]);
				max[pos] = nums[i];
				maxindex[pos] = i;
			}
		}
		for(int i=0;i<len;i++){
			System.out.print(nums[maxindex[i]]+" ");
		}
		System.out.println();
		System.out.println(len);
	}
	public static int binarysearch(int[] max,int left,int right,int key){
		while(left<=right){
			int mid = (right - left) / 2 + left;
			if(max[mid] == key){
				return mid;
			}
			else if(max[mid]<key){
				left = mid+1;
			}else{
				right = mid-1;
			}
		}
		return left;
	}

}


3、最大子序列和

public class sf_dp_MaxSum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] nums = {-3,-4,-2,-6};
		int len = nums.length;
		int maxsum = nums[0];
		int cursum = nums[0];
		for(int i=1;i<len;i++){
			if(cursum<0){
				cursum = nums[i];
			}
			else{
				cursum += nums[i];
			}
			maxsum = Math.max(cursum, maxsum);
		}
		System.out.println(maxsum);
	}

}


4、合唱队

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            int[] nums = new int[n];
            for(int i=0;i<n;i++){
                nums[i] = in.nextInt();
            }
            System.out.println(fun(nums,n));
        }
    }
    public static int fun(int[] nums,int n){
        int[] len1 = new int[n]; //从左到右
        int[] len2 = new int[n]; //从右到左
        for(int k=0;k<n;k++){ //初始化2个数组
            len1[k] = 1;
            len2[k] = 1;
        }
        for(int i=1;i<n;i++){  //求最长递增子序列的方法
            for(int j=0;j<i;j++){
                if(nums[i]>nums[j]&&len1[i]<len1[j]+1)
                    len1[i] = len1[j]+1;
            }
        }
        for(int i=n-2;i>=0;i--){ //求最长递减子序列(其实就是反向求最长递增子序列)
            for(int j=n-1;j>i;j--){
                if(nums[i]>nums[j]&&len2[i]<len2[j]+1)
                    len2[i] = len2[j]+1;
            }
        }
        int max = 0;
        for(int k=0;k<n;k++){
            if(max<len1[k]+len2[k]-1)  //还要减一,因为中间的数算了2次
                max = len1[k]+len2[k]-1;
        }
        return n-max;
    }
}

5、最长回文子串长度

public class LongestPalindromic {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "abcdcbaaweewdd";
		int len = str.length();
		int max = 0;
		int left = 0;
		int right = 0;
		boolean[][] c = new boolean[len][len];
		for(int i=0;i<len;i++){
			for(int j=0;j<len;j++){
				if(i>=j)
					c[i][j] = true;
				else
					c[i][j] = false;
			}
		}
		for(int j=1;j<len;j++){
			for(int i=0;i<j;i++){
				if(str.charAt(i)==str.charAt(j)&&c[i+1][j-1]){
					c[i][j] = true;
					if(j-i+1>max){
						max = j-i+1;
						left = i;
						right = j;
					}
				}
				else{
					c[i][j] = false;
				}
			}
		}
		System.out.println(max);
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值