Interleaving String

来源:互联网 发布:王牌特工2 知乎 编辑:程序博客网 时间:2024/06/02 09:20

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

动态规划:

class Solution {public:  bool isInterleave(string s1, string s2, string s3) {        // Start typing your C/C++ solution below        // DO NOT write int main() function            if (s1.empty() || s2.empty()) {           return s1 == s3 || s2 == s3;        }        if (s3.size() != s1.size() + s2.size()) {           return false;        }                vector<vector<bool> > record;        int m = s1.size();        int n = s2.size();        for (int i = 0; i < m+1; ++i) {           record.push_back(vector<bool>(n+1, false));        }        record[0][0] = true;        for (int i = 0; i <= m; ++i) {            for (int j = 0; j <= n; ++j) {                if (i != 0 && j != 0) {                   record[i][j] = (record[i-1][j] && s1[i-1] == s3[i+j-1])                               || (record[i][j-1] && s2[j-1] == s3[i+j-1]);                } else if (i != 0) {                    record[i][j] = record[i-1][j] && s1[i-1] == s3[i+j-1];                } else if (j != 0) {                    record[i][j] = record[i][j-1] && s2[j-1] == s3[i+j-1];                }            }        }        return record[m][n];    }};


回溯:

class Solution {typedef struct record {     record() : p1(NULL), p2(NULL), step(0) {}     record(const char* s1, const char* s2, int s) : p1(s1), p2(s2), step(s) {}     const char* p1;     const char* p2;     int step; }Record;public:    bool isInterleave(string s1, string s2, string s3) {        // Start typing your C/C++ solution below        // DO NOT write int main() function            if (s1.size() + s2.size() != s3.size()) {            return false;        }        const char* p = s3.c_str();        const char* p1 = s1.c_str();        const char* p2 = s2.c_str();        stack<Record> st;        int step = 0;        bool back = false;        int back_step = 0;        Record r;        while (*p) {            if (*p1 == *p2 && *p1 == *p && !back) {          //    step += back_step;                st.push(Record(p1, p2, step));                step = 0;                back_step = 0;            }                        if (*p1 == *p && !back) {                ++p;                ++p1;                ++step;                back = false;                continue;            }                        if (*p2 == *p) {                ++p;                ++p2;                ++step;                back = false;                continue;            }            if (*p1 =='\0' || *p2 == '\0') {                p1 = *p1 == '\0' ? p2 : p1;                while(*p && *p == *p1) {                  ++p1;                  ++p;                  ++step;                }            }                        if (*p) {              if(st.empty()) {                  return false;              } else {                  back = true;                  r = st.top();                  p1 = r.p1;                  p2 = r.p2;                  step += back_step;                  p -= step;                  step = 0;                  back_step = r.step;                  st.pop();              }            }        }        return true;     }};

Debug版:

// Type your C++ code and click the "Run Code" button!// Your code output will be shown on the left.// Click on the "Show input" button to enter input data to be read (from stdin).#include <iostream>using namespace std;bool isInterleave(string s1, string s2, string s3) {        // Start typing your C/C++ solution below        // DO NOT write int main() function            if (s1.size() + s2.size() != s3.size()) {            cout << "F-0" << endl;            return false;        }        const char* p = s3.c_str();        const char* p1 = s1.c_str();        const char* p2 = s2.c_str();        stack<const char*> st1;        stack<const char*> st2;        stack<int> steps;        int step = 0;        bool back = false;        int back_step = 0;        while (*p) {            if (*p1 == *p2 && *p1 == *p && !back) {                st1.push(p1);                st2.push(p2);           //     cout << step << " " << *p1 << " Push: s1." << p1 - s1.c_str() << "  s2." << p2 - s2.c_str() << endl;                 step += back_step;                steps.push(step);                back_step = 0;                step = 0;            }                        if (*p1 == *p && !back) {           //     cout <<*p << " Macth: s3." << p - s3.c_str() << " <--> s1." << p1 - s1.c_str() << endl;                 ++p;                ++p1;                ++step;                back = false;                continue;            }                        if (*p2 == *p) {           //    cout << *p << " Macth: s3." << p - s3.c_str() << " <--> s2." << p2 - s2.c_str() << endl;                 ++p;                ++p2;                ++step;                back = false;                continue;            }            if (*p1 =='\0' || *p2 == '\0') {                p1 = *p1 == '\0' ? p2 : p1;                while(*p && *p == *p1) {                  ++p1;                  ++p;                  ++step;                  back = false;                }            }                        if (*p) {              if(st1.empty()) {                  cout << "xxxxx" << endl;                  return false;              } else {               //   cout << endl << "-------------" << endl;               //   cout << *p << " From: s3." << p - s3.c_str() << " s1." << p1 - s1.c_str() << " s2." << p2 - s2.c_str() << endl;                //   cout << *p << " From: s3." << *p << " s1." << *p1 << " s2." << *p2 << endl;                   back = true;                  p1 = st1.top();                  st1.pop();                  p2 = st2.top();                  st2.pop();            //      cout << "cur_step:" << step << endl << "back_step:" << back_step << endl;                   step += back_step;             //     cout << "total_step:" << step << endl;                  p -= step;                  step = 0;                  back_step = steps.top();              //    cout << "back_step:" << back_step << endl;                  steps.pop();               //   cout << *p << " Back: s3." << p - s3.c_str() << " s1." << p1 - s1.c_str() << " s2." << p2 - s2.c_str() << endl;                //   cout << *p << " Back: s3." << *p << " s1." << *p1 << " s2." << *p2 << endl;               //    cout << "-------------" << endl << endl;              }            }        }        return true;     }    int main() {    // Start typing your code here...        cout << isInterleave(    "bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa",     "babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab",     "babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab") << endl;            return 0;}



原创粉丝点击