LeetCode Regular Expression Matching

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   div   sp   log   

class Solution {#define SINGLE 1#define MULTIP 2public:    bool isMatch(const char *s, const char *p) {        if (s == NULL || p == NULL) return true;                vector<pair<char, int> > pattern;                int     pos = 0;        char     ch = ‘\0‘;                while ((ch = p[pos++]) != ‘\0‘) {            if (ch == ‘*‘) {                pattern.back().second = MULTIP;                continue;            }            pattern.push_back(make_pair(ch, SINGLE));        }                return isMatch(s, 0, pattern, 0);    }        bool isMatch(const char *s, int pos, vector<pair<char, int> > &pattern, int ppos) {        if (ppos == pattern.size()) {            return s[pos] == ‘\0‘;        }        int i = pos;        pair<char, int> &p = pattern[ppos];        int     offset = (p.second == SINGLE) ? 0 : -1;        char     ch = ‘\0‘;        while (offset < 0 || (ch = s[pos + offset]) != ‘\0‘) {            if (offset >= 0 && !cmp(ch, p.first)) {                return false;            }            if (isMatch(s, pos + offset + 1, pattern, ppos + 1)) {                return true;            }            if (p.second == SINGLE) break;            offset++;        }        return false;    }        bool cmp(const char a, const char b) {        if (a == ‘.‘ || b == ‘.‘) return true;        return a == b;    }};

暴力法, 216ms,

 

 

改寫為記憶搜尋,這裡假設字串和模式字串的長度都不超過unsigned short 範圍(6w+), 84ms

class Solution {#define SINGLE 1#define MULTIP 2private:    unordered_map<unsigned int, bool> memo;public:    bool isMatch(const char *s, const char *p) {        if (s == NULL || p == NULL) return true;        memo.clear();                vector<pair<char, int> > pattern;                int     pos = 0;        char     ch = ‘\0‘;                while ((ch = p[pos++]) != ‘\0‘) {            if (ch == ‘*‘) {                pattern.back().second = MULTIP;                continue;            }            pattern.push_back(make_pair(ch, SINGLE));        }                return isMatch(s, 0, pattern, 0);    }        bool isMatch(const char *s, int pos, vector<pair<char, int> > &pattern, int ppos) {        unsigned int id = (pos << 16) | ppos;                unordered_map<unsigned int, bool>::iterator iter = memo.find(id);        if (memo.find(id) != memo.end()) return iter->second;                bool res = false;                if (ppos == pattern.size()) {            res = s[pos] == ‘\0‘;            memo.insert(make_pair(id, res));            return res;        }        int i = pos;        pair<char, int> &p = pattern[ppos];        int     offset = (p.second == SINGLE) ? 0 : -1;        char     ch = ‘\0‘;        while (offset < 0 || (ch = s[pos + offset]) != ‘\0‘) {            if (offset >= 0 && !cmp(ch, p.first)) {                res = false;                break;            }            if (isMatch(s, pos + offset + 1, pattern, ppos + 1)) {                res = true;                break;            }            if (p.second == SINGLE) break;            offset++;        }        memo.insert(make_pair(id, res));        return res;    }        bool cmp(const char a, const char b) {        if (a == ‘.‘ || b == ‘.‘) return true;        return a == b;    }};

 

LeetCode Regular Expression Matching

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.