8.16 dp decodeWays & distinctSubsequence & wordBreak I-II

- to do

- 13.10] Decode Ways

  • totally ignored the special case...! (think what it is)
  • dp[i] stores #ways to decode s[0~i-1]
  • group cases together according to logical meaning (e.g. cuz last two digits isn't valid code?)
    int numDecodings(string s) {
        int n = s.size();
        if (n==0 || s[0]=='0') return 0;
        vector dp(n+1, 1);
        for (int i=2; i26) return 0;
                else dp[i] = dp[i-2];
            else // 
                dp[i] = lastDigit==0 || lastDigit*10 + currDigit>26? dp[i-1] : dp[i-1]+dp[i-2];
        }
        return dp[n];
    }

- 13.11] Word Break

top-down的备忘录法,这个没怎么做过习惯一下格式;另外substr(starti, count), 老是写成range。。

    bool dfs(string& s, unordered_set& wordDict, vector& dp, int index) {
        if (dp[index] != -1) return dp[index]==1? true : false;
        int restLen = s.size()-index;
        for (auto& entry : wordDict) {
            if (entry.size() > restLen || entry.compare(s.substr(index, entry.size()))!=0) {
                continue;
            }
            if (dfs(s, wordDict, dp, index+entry.size())) {
                dp[index] = 1;
                return true;
            }
        }
        dp[index] = 0;
        return false;
    }
    bool wordBreak(string s, unordered_set& wordDict) {
        // dp[i] tells if s[i~n-1] is solvable(1; solvable; 0:not)
        vector dp(s.size()+1, -1);
        dp[s.size()] = 1;
        return dfs(s, wordDict, dp, 0);
    }

- dp的word break

一遍通过的感觉棒棒哒~~

    bool wordBreak(string s, unordered_set& wordDict) {
        int n = s.size();
        vector dp(n+1, false);
        dp[n] = true;
        for (int i=n-1; i>=0; --i) {
            string curr = s.substr(i, s.size()-i);
            for (auto& entry : wordDict) {
                if (entry.size() > curr.size() 
                || entry.compare(curr.substr(0, entry.size()))!=0) continue;
                if (dp[i+entry.size()]) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[0];
    }

- https://leetcode.com/problems/word-break-ii/

out of memory :(

    vector wordBreak(string s, unordered_set& wordDict) {
        int n = s.size();
        if (!n) return vector{};
        vector> dp(n, vector{});
        // dp[i] stores possible sentences using s[i~n-1]
        for (int i=n-1; i>=0; --i) {
            string curr = s.substr(i, s.size()-i); 
            for (auto& entry : wordDict) {
                if (entry.size() > curr.size() || entry.compare(s.substr(i, entry.size()))!=0) continue;
                if (i+entry.size() == n) dp[i].push_back(entry);
                else if (!dp[i+entry.size()].empty()) {
                    for (auto& restStr: dp[i+entry.size()]) {
                        dp[i].push_back(entry+" "+restStr);
                    }
                }
            }
        }
        return dp[0]; 
    }

mark 超时。。。dfs为什么。。

    vector wordBreak(string s, unordered_set& wordDict) {
        int n = s.size();
        if (!n) return vector{};
        
        vector> dp(n, vector{});
        // dp[i] stores words that are prefixes of s[i~n-1]
        for (int i=n-1; i>=0; --i) {
            // cout<curr.size() || word!=curr.substr(0, word.size())) continue;
                dp[i].push_back(word);
                // cout<<" "< ret;
        dfs(n, 0, dp, "", ret);
        return ret;
    }
    
        void dfs(int n, int next, vector>& dp, string path, vector& ret) {
            if (next>=n) {
                ret.push_back(path);
                return;
            }
            for (auto& word: dp[next]) {
                string newp = path==""? word :  path+" "+word;
                dfs(n, next+word.size(), dp, newp, ret);
            }
        } 

你可能感兴趣的:(8.16 dp decodeWays & distinctSubsequence & wordBreak I-II)