Leetcode Regular Expression Matching

Leetcode Regular Expression Matching,本题的主要思路是回溯法,算法主要需要处理两种状态:
1. 正常字符时,如’a-z’, ‘.’等, 这类直接匹配。
2. 遇到”x*”形式时,需要使用回溯法,进行逐个测试,直接找到一个可用的子匹配,否非反回false。

相关算法如下,以下算法效率不高,还有很多可优化的地方:

#include<iostream>
#include<vector>
#include<string>
#include<map>

/**
 * The method to solve this problem is backtrack.
 * First match the pure characters include '.'
 * Second try the "x*" pattern, if find a match return true, if not return false
 *
 */
using namespace std;
class Solution {
public:
    bool isMatch(string s, string p) {
        int pos = 0;
        // remove the duplicated "*"
        for (int i = 0; i < p.length(); i++) {
            if (!(p[i] == '*' && (i == 0 || p[i - 1] == '*'))) {
                p[pos] = p[i];
                pos++;
            }
        }
        p = p.substr(0, pos);
        return match(s, 0, p, 0);
    }
    bool match(string& s, int idxs, string& p, int idxp) {
        // make sure the there are more characters need to match
        if (idxs >= s.length() && idxp >= p.length()) {
            return true;
        } else if (idxp >= p.length()) {
            return false;
        }

        // match the pure character
        while (idxs < s.length() && idxp < p.length() - 1
                && p[idxp + 1] != '*') {
            if (p[idxp] != s[idxs] && p[idxp] != '.') {
                return false;
            }
            idxp++;
            idxs++;
        }

        // make sure after the preceding match, there state is p encounter the
        // end or the s encounter the end and the left p pattern is "x*xxxx"
        if (idxp == p.length() - 1) {
            if (idxs == s.length() - 1 &&
                    (p[idxp] == s[idxs] || p[idxp] == '.')) {
                return true;
            } else {
                return false;
            }
        } else if (idxs >= s.length() && p[idxp + 1] != '*') {
            return false;
        }

        // process the "x*" using backtrack
        int idx = idxs;
        while (idx < s.length() && (p[idxp] == s[idx] || p[idxp] == '.')) {
            if (match(s, idx, p, idxp + 2)) {
                return true;
            }
            idx++;
        }
        if (idx == s.length() && idxp + 2 == p.length() && p[idxp + 1] == '*') {
            return true;
        }
        return match(s, idx, p, idxp + 2);
    }

};

int main(int argc, char* argv[]) {
    Solution so;
    cout<<"result: "<<so.isMatch(string(argv[1]), string(argv[2]))<<endl;
    return 0;
}
测试:
./a.out "abcabcabc" "a*b*c*abcabc"
result: 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值