zoukankan      html  css  js  c++  java
  • LeetCode "Interleaving String"

    I briefly took a look at: http://yucoding.blogspot.com/2013/01/leetcode-question-27-interleaving-string.html And I got 1A :)

    This is the best problem to understand DP deeper. At the very first sight to this problem statement, DP pattern may not be that obvious. Yes, DP is hard. But again, you should be able to figure out a DFS solution. So, please revise: what is the repeating computation pattern of the DFS solution? Right, s1[i] & s2[j] may be computed multiple times, when backtrace occurs in your DFS. And here comes the punch line of this problem:

    DP pattern should address your enumerationDFS repeating computation pattern! That's exactly the purpose of DP!

    Here's the code:

    class Solution {
    public:
        bool isInterleave(string s1, string s2, string s3) {
            size_t len1 = s1.length();
            size_t len2 = s2.length();
            size_t len3 = s3.length();
            if ((len1 + len2) != len3) return false;
    
            //    Init
            vector<bool> ret; ret.resize(len3 + 1);
            std::fill(ret.begin(), ret.end(), false);
    
            vector<vector<bool>> dp;    //    dp[i][j]: s1: len i, s2: len j -> valid?
            dp.resize(len2 + 1);
            for (int i = 0; i < len2 + 1; i++)
            {
                dp[i].resize(len1 + 1);
                std::fill(dp[i].begin(), dp[i].end(), false);
            }
    
            //    Go DP: 
            //        dp[i + 1][j] = dp[i][j] ? (s[i + j] == s1[i]) : false
            //        then, ret[i + j + 1] = dp[i + 1][j]
            for (int i = 0; i <= len1; i ++)
            for (int j = 0; j <= len2; j++)
            {
                if (i == 0 && j == 0)
                {
                    dp[i][j] = true;
                    ret[0] = true;
                    continue;
                }
    
                if (i == 0)
                {
                    dp[j][i] = dp[j - 1][i] && (s3[j - 1] == s2[j - 1]);
                }
                else if (j == 0)
                {
                    dp[j][i] = dp[j][i - 1] && (s3[i - 1] == s1[i - 1]);
                }
                else
                {
                    bool bi = dp[j - 1][i] && (s3[i + j - 1] == s2[j - 1]);
                    bool bj = dp[j][i - 1] && (s3[i + j - 1] == s1[i - 1]);
                    dp[j][i] = dp[j][i] | bi | bj;
                }
    
                ret[i + j] = ret[i + j] | dp[j][i];
            }
            return ret[len3];
        }
    };
  • 相关阅读:
    Java多线程(二)关于多线程的CPU密集型和IO密集型这件事
    Java 过一下基础
    日历打印用java实现
    DAY8-打卡第八天-2018-1-18
    web-day1-初识标识符
    DAY7-图形界面第一弹-2018-1-17
    四种排序方法用java实现
    DAY6-小变化(java提示框)-2018-1-16
    DAY5-小别-2018-1-15
    DAY4-打卡第四天-2018-1-12
  • 原文地址:https://www.cnblogs.com/tonix/p/3904425.html
Copyright © 2011-2022 走看看