笔试题-动态规划系列

来源:互联网 发布:倒卖车辆数据 编辑:程序博客网 时间:2024/05/22 07:47

1、 最长递增子序列

时间限制:3秒
空间限制:32768K
对于一个数字序列,请设计一个复杂度为O(nlogn)的算法,返回该序列的最长上升子序列的长度,这里的子序列定义为这样一个序列U1,U2…,其中Ui < Ui+1,且A[Ui] < A[Ui+1]。
给定一个数字序列A及序列的长度n,请返回最长上升子序列的长度。
测试样例:
[2,1,4,3,1,5,6],7
返回:4

private static int getNum(int[] A, int n) {        int[] B=new int[n];        int end=0;//记录B中最后一个数的下标        B[0]=A[0];        for(int i=1;i<n;i++){            if(A[i]>=B[end]){                B[++end]=A[i];                continue;            }            //否则,需要先找替换位置            int pos=findInsertPos(B, A[i], 0, end);            B[pos]=A[i];        }        for(int i=0;i<B.length;++i){            System.out.println(B[i]);        }        return end+1;    }    //二分查找第一个大于等于n的位置    public  static int findInsertPos(int[] B,int n,int start,int end){        while(start<end){            int mid=start+(end-start)/2;//直接使用(high+low)            if(B[mid]<n){                start=mid+1;            }else if(B[mid]>n){                end=mid;            }else{                return mid;            }        }        return start;           }

2、最长公共子序列

时间限制:3秒
空间限制:32768K
对于两个字符串,请设计一个高效算法,求他们的最长公共子序列的长度,这里的最长公共子序列定义为有两个序列U1,U2,U3…Un和V1,V2,V3…Vn,其中Ui&ltUi+1,Vi&ltVi+1。且A[Ui] == B[Vi]。
给定两个字符串A和B,同时给定两个串的长度n和m,请返回最长公共子序列的长度。保证两串长度均小于等于300。
测试样例:
“1A2C3D4B56”,10,”B1D23CA45B6A”,12
返回:6

    private static int getNum(String str1, String str2) {        char[] ch1=str1.toCharArray();        char[] ch2=str2.toCharArray();        int[][] c=new int[str1.length()+1][str2.length()+1];        for(int i=0;i<=str2.length();i++){            c[0][i]=0;        }        for(int i=0;i<=str1.length();i++){            c[i][0]=0;        }        for(int i=1;i<=str1.length();i++){            for(int j=1;j<=str2.length();j++){                if(ch1[i-1]==ch2[j-1]){                    c[i][j]=c[i-1][j-1]+1;                }else {                    c[i][j]=Math.max(c[i][j-1], c[i-1][j]);                }            }        }        return c[str1.length()][str2.length()];    }

3、最长公共子串

时间限制:3秒
空间限制:32768K
对于两个字符串,请设计一个时间复杂度为O(m*n)的算法(这里的m和n为两串的长度),求出两串的最长公共子串的长度。这里的最长公共子串的定义为两个序列U1,U2,..Un和V1,V2,…Vn,其中Ui + 1 == Ui+1,Vi + 1 == Vi+1,同时Ui == Vi。
给定两个字符串A和B,同时给定两串的长度n和m。
测试样例:
“1AB2345CD”,9,”12345EF”,7
返回:4

private static int getNum(String str1, String str2, int n, int m) {        char[] ch1 = str1.toCharArray();        char[] ch2 = str2.toCharArray();        int max = 0;        int[][] c = new int[n + 1][m + 1];        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= m; j++) {                if (ch1[i - 1] == ch2[j - 1]) {                    c[i][j] = c[i - 1][j - 1] + 1;                    if (c[i][j] > max) {                        max = c[i][j];                    }                } else {                    c[i][j] = 0;                }            }        }        return max;    }

4、最小编辑代价

时间限制:3秒
空间限制:32768K
对于两个字符串A和B,我们需要进行插入、删除和修改操作将A串变为B串,定义c0,c1,c2分别为三种操作的代价,请设计一个高效算法,求出将A串变为B串所需要的最少代价。
给定两个字符串A和B,及它们的长度和三种操作代价,请返回将A串变为B串所需要的最小代价。保证两串长度均小于等于300,且三种代价值均小于等于100。
测试样例:
“abc”,3,”adc”,3,5,3,100
返回:8

//最小编辑代价http://blog.csdn.net/u013328850/article/details/53187870public class MinCost {    public static void main(String[] args) {        System.out.println(findMinCost("abc", 3, "adc", 3, 5, 3, 100));    }    public static int findMinCost(String A, int n, String B, int m, int c0, int c1, int c2) {        char[] str1 = A.toCharArray();        char[] str2 = B.toCharArray();        int[][] dp = new int[n + 1][m + 1];        dp[0][0] = 0;        for (int j = 1; j < m + 1; j++) {            dp[0][j] = c0 * j;        }        for (int i = 1; i < n + 1; i++) {            dp[i][0] = c1 * i;        }        for (int i = 1; i < n + 1; i++) {            for (int j = 1; j < m + 1; j++) {                if (str1[i - 1] == str2[j - 1]) {                    dp[i][j] = dp[i - 1][j - 1];                } else {                    dp[i][j] = dp[i - 1][j - 1] + c2;                }                dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + c1);                dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + c0);            }        }        return dp[n][m];    }}

5、字符串交错组成

时间限制:3秒
空间限制:32768K
对于三个字符串A,B,C。我们称C由A和B交错组成当且仅当C包含且仅包含A,B中所有字符,且对应的顺序不改变。请编写一个高效算法,判断C串是否由A和B交错组成。
给定三个字符串A,B和C,及他们的长度。请返回一个bool值,代表C是否由A和B交错组成。保证三个串的长度均小于等于100。
测试样例:
“ABC”,3,”12C”,3,”A12BCC”,6
返回:true

public  static boolean chkMixture(String A, int n, String B, int m, String C, int v) {        if(n+m!=v||getNum(A,C)!=n||getNum(B,C)!=m){            return false;        }        return true;    }     //最长公共子序列    private static int getNum(String str1, String str2) {        char[] ch1 = str1.toCharArray();        char[] ch2 = str2.toCharArray();        int[][] c = new int[str1.length() + 1][str2.length() + 1];        for (int i = 0; i <= str2.length(); i++) {            c[0][i] = 0;        }        for (int i = 0; i <= str1.length(); i++) {            c[i][0] = 0;        }        for (int i = 1; i <= str1.length(); i++) {            for (int j = 1; j <= str2.length(); j++) {                if (ch1[i - 1] == ch2[j - 1]) {                    c[i][j] = c[i - 1][j - 1] + 1;                } else {                    c[i][j] = Math.max(c[i][j - 1], c[i - 1][j]);                }            }        }        return c[str1.length()][str2.length()];    }
原创粉丝点击