动态规划----各种最长序列

来源:互联网 发布:闲鱼恶意退款 淘宝介入 编辑:程序博客网 时间:2024/05/22 06:33

最大连续子数组和

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

click to show more practice.

More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

int maxSubArray(vector<int>& nums) {    if (nums.size() == 0)return 0;    int sum = nums[0];    int maxsum = sum;    for (int i = 1; i < nums.size(); i++){        sum = max(nums[i], nums[i] + sum);        maxsum = max(maxsum, sum);    }    return maxsum;}
int maxSubArray(vector<int>& nums) {    if (nums.size() == 0)return 0;    int sum = nums[0];    int maxsum = sum;    for (int i = 1; i < nums.size(); i++){        sum = sum < 0 ? nums[i] : sum + nums[i];        maxsum = max(sum, maxsum);    }    return maxsum;}

最长上升子序列

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

int lengthOfLIS(vector<int>& nums) {    if (nums.size() == 0)return 0;    vector<int> dp(nums.size(), 0);    dp[0] = nums[0];    int len = 1;    for (int i = 1; i < nums.size(); i++){        int idx = lower_bound(dp.begin(), dp.begin() + len, nums[i]) - dp.begin();        if (idx == len)len++;        dp[idx] = nums[i];    }    return len;}

最长公共子序列

给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。

样例

给出”ABCD” 和 “EDCA”,这个LCS是 “A” (或 D或C),返回1
给出 “ABCD” 和 “EACB”,这个LCS是”AC”返回 2

int longestCommonSubsequence(string A, string B) {    if (A == "" || B == "")return 0;    vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1));    int len1 = A.size(), len2 = B.size();    for (int i = 1; i <= A.size(); i++){        for (int j = 1; j <= B.size(); j++){            if (A[i - 1] == B[j - 1])                dp[i][j] = dp[i - 1][j - 1] + 1;            else                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);        }    }    return dp[len1][len2];}

编辑距离

给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数。

你总共三种操作方法:

插入一个字符
删除一个字符
替换一个字符

样例
给出 work1=”mart” 和 work2=”karma”

返回 3

int minDistance(string word1, string word2) {    int len1 = word1.size(), len2 = word2.size();    vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1));    for (int i = 1; i <= len1; i++)dp[i][0] = i;    for (int j = 1; j <= len2; j++)dp[0][j] = j;    for (int i = 1; i <= len1; i++){        for (int j = 1; j <= len2; j++){            if (word1[i - 1] == word2[j - 1])dp[i][j] = dp[i - 1][j - 1];            else dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;        }    }    return dp[len1][len2];}

最长回文子串

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:Input: "babad"Output: "bab"

Note: “aba” is also a valid answer.

Example:Input: "cbbd"Output: "bb"

针对某个当前点,以当前点为中心,找出最长的回文子串;

string Palsubstr(string& str, int low, int high){    while (low >= 0 && high < str.size() && str[low] == str[high]){        low--, high++;    }    return str.substr(low + 1, high - low - 1);}string longestPalindrome(string s) {    if (s == "")return "";    int center = 0, max_len = 0;    string maxstr = s.substr(0, 1);    for (int i = 0; i < s.size(); i++){        string str1 = Palsubstr(s, i, i);        string str2 = Palsubstr(s, i, i + 1);        int len1 = str1.size(), len2 = str2.size();        if (max(len1, len2) > maxstr.size()){            if (len1 > len2)maxstr = str1;            else maxstr = str2;        }    }    return maxstr;}
原创粉丝点击