lintcode DP 总结

107. Word BreakFollow

https://www.lintcode.com/problem/word-break/description

Description

中文English

Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words.

Have you met this question in a real interview?  Yes

Problem Correction

Example

Example 1:
	Input:  "lintcode", ["lint", "code"]
	Output:  true

Example 2:
	Input: "a", ["a"]
	Output:  true
	

这个必须得先求一下最长的子串长度, 要不会TLE

class Solution {
public:
    /*
     * @param s: A string
     * @param dict: A dictionary of words dict
     * @return: A boolean
     */
    int maxLength(unordered_set<string> &dict) {
        int maxLength = 0; 
        for (unordered_set<string>::iterator it = dict.begin(); it != dict.end(); ++it) { 
            maxLength = maxLength > (*it).length() ? maxLength : (*it).length();
        }
        return maxLength;
    }
    bool wordBreak(string &s, unordered_set<string> &dict) {
        // write your code here
        int len = s.length();
        vector<bool>flag(len+1, false);
        flag[0] = true;
        int maxLen = maxLength(dict);
        for (int i = 1; i <= len; i ++) {
            for(int j = 1; j <= maxLen && j <= i; j ++) {
                if (flag[i-j] && dict.find(s.substr(i-j, j)) != dict.end()) {
                    flag[i] = true;
                    break;
                }
            }
        }
        return flag[len];
    }
};

108. Palindrome Partitioning

https://www.lintcode.com/problem/palindrome-partitioning-ii/description

Description

中文English

Given a string s, cut s into some substrings such that every substring is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Have you met this question in a real interview?  Yes

Problem Correction

Example

Example 1:

Input: "a"
Output: 0
Explanation: "a" is already a palindrome, no need to split.

Example 2:

Input: "aab"
Output: 1
Explanation: Split "aab" once, into "aa" and "b", both palindrome.
class Solution {
public:
    /**
     * @param s: A string
     * @return: An integer
     */
    bool judge(string s) {
        int len = s.length();
        for (int i = 0; i < len/2; i ++) {
            if (s[i] != s[len - i - 1]) {
                return false;
            }
        }
        return true;
    }
    int minCut(string &s) {
        // write your code here
        int len = s.length();
        vector<int>v(len+1, 0x3f3f3f3f);
        v[0] = -1;
        for (int i = 1; i <= len; i ++) {
            for (int j = 1; j <= i; j ++) {
                if (v[i-j] != 0x3f3f3f3f && judge(s.substr(i-j, j))) {
                    v[i] = min(v[i], v[i-j] + 1);
                }
            }
        }
        return v[len];
    }
};

118. Distinct Subsequences

https://www.lintcode.com/problem/distinct-subsequences/description

Description

中文English

Given two strings S and T. Count the number of distinct subsequences of S which equals T.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not)

Have you met this question in a real interview?  Yes

Problem Correction

Example

Example 1:

Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation: You could remove any 'b' in S, so there are 3 ways to get T.

Example 2:

Input: S = "abcd", T = ""
Output: 1
Explanation: There is only 1 way to get T - remove all chars in S.
class Solution {
public:
    /**
     * @param S: A string
     * @param T: A string
     * @return: Count the number of distinct subsequences
     */
    int numDistinct(string &S, string &T) {
        // write your code here
        int lenS = S.length();
        int lenT = T.length();
        //vector<vector <int> > v(lenS+1, vector<int>(lenT+1, 0));
        vector<vector <int> > v(2, vector<int>(lenT+1, 0));
        for (int i = 0; i <= lenS; i ++) {
            for (int j = 0; j <= lenT; j ++) {
                if (i == 0 && j == 0) {
                    v[i%2][j] = 1;
                } else if (i == 0) {
                    v[i%2][j] = 0;
                } else if (j == 0) {
                    v[i%2][j] = 1;
                } else {
                    v[i%2][j] = 0;
                    if (S[i-1] == T[j-1]) {
                        v[i%2][j] += v[(i-1)%2][j-1];
                    }
                    v[i%2][j] += v[(i-1)%2][j];
                }
            }
        }
        return v[lenS%2][lenT];
    }
};

注意初始化0,要在for里面 不能用旧的数据。

https://leetcode.com/problems/edit-distance/

Edit distance

class Solution {
public:
    int minDistance(string word1, string word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        if (len1 == 0) return len2;
        if (len2 == 0) return len1;
        vector<vector <int> > v(len1+1 ,vector<int>(len2+1,0x3f3f3f3f));
        //vector< int, vector<int> > v(lenS, vector<int>(lenT,0));
        for (int i = 0; i <= len1; i ++) v[i][0] = i;
        for (int i = 0; i <= len2; i ++) v[0][i] = i;
     //   if (word1[0] == word2[0]) v[0][0] = 0;
        for (int i = 1; i <= len1; i ++) {
            for (int j = 1; j <= len2; j ++) {
                if(word1[i-1] == word2[j-1]) {
                    v[i][j] = v[i-1][j-1];
                } else {
                    v[i][j] = min(v[i-1][j-1], min(v[i-1][j], v[i][j-1])) + 1;
                }
            }
        }
        return v[len1][len2];
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值