码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Regular Expression Matching

时间:2014-09-14 01:20:56      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   div   sp   log   

class Solution {
#define SINGLE 1
#define MULTIP 2
public:
    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 2
private:
    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

标签:style   blog   color   io   os   ar   div   sp   log   

原文地址:http://www.cnblogs.com/lailailai/p/3970564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!