溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么實現(xiàn)正則表達(dá)式匹配

發(fā)布時間:2022-03-28 10:27:47 來源:億速云 閱讀:217 作者:iii 欄目:大數(shù)據(jù)

這篇“C++怎么實現(xiàn)正則表達(dá)式匹配”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C++怎么實現(xiàn)正則表達(dá)式匹配”文章吧。

Regular Expression Matching 正則表達(dá)式匹配

Given an input string (s) and a pattern (p), implement regular expression matching with support for "." and "*".

"." Matches any single character.
"*" Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.

  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: "*" means zero or more of the precedeng element, "a". Therefore, by repeating "a" once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

這道求正則表達(dá)式匹配的題和那道 Wildcard Matching 的題很類似,不同點在于*的意義不同,在之前那道題中,*表示可以代替任意個數(shù)的字符,而這道題中的*表示之前那個字符可以有0個,1個或是多個,就是說,字符串 a*b,可以表示b或是 aaab,即a的個數(shù)任意,這道題的難度要相對之前那一道大一些,分的情況的要復(fù)雜一些,需要用遞歸 Recursion 來解,大概思路如下:

- 若p為空,若s也為空,返回 true,反之返回 false。

- 若p的長度為1,若s長度也為1,且相同或是p為 "." 則返回 true,反之返回 false。

- 若p的第二個字符不為*,若此時s為空返回 false,否則判斷首字符是否匹配,且從各自的第二個字符開始調(diào)用遞歸函數(shù)匹配。

- 若p的第二個字符為*,進(jìn)行下列循環(huán),條件是若s不為空且首字符匹配(包括 p[0] 為點),調(diào)用遞歸函數(shù)匹配s和去掉前兩個字符的p(這樣做的原因是假設(shè)此時的星號的作用是讓前面的字符出現(xiàn)0次,驗證是否匹配),若匹配返回 true,否則s去掉首字母(因為此時首字母匹配了,我們可以去掉s的首字母,而p由于星號的作用,可以有任意個首字母,所以不需要去掉),繼續(xù)進(jìn)行循環(huán)。

- 返回調(diào)用遞歸函數(shù)匹配s和去掉前兩個字符的p的結(jié)果(這么做的原因是處理星號無法匹配的內(nèi)容,比如 s="ab", p="a*b",直接進(jìn)入 while 循環(huán)后,我們發(fā)現(xiàn) "ab" 和 "b" 不匹配,所以s變成 "b",那么此時跳出循環(huán)后,就到最后的 return 來比較 "b" 和 "b" 了,返回 true。再舉個例子,比如 s="", p="a*",由于s為空,不會進(jìn)入任何的 if 和 while,只能到最后的 return 來比較了,返回 true,正確)。

解法一:

class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.empty()) return s.empty();
        if (p.size() == 1) {
            return (s.size() == 1 && (s[0] == p[0] || p[0] == "."));
        }
        if (p[1] != "*") {
            if (s.empty()) return false;
            return (s[0] == p[0] || p[0] == ".") && isMatch(s.substr(1), p.substr(1));
        }
        while (!s.empty() && (s[0] == p[0] || p[0] == ".")) {
            if (isMatch(s, p.substr(2))) return true;
            s = s.substr(1);
        }
        return isMatch(s, p.substr(2));
    }
};

上面的方法可以寫的更加簡潔一些,但是整個思路還是一樣的,先來判斷p是否為空,若為空則根據(jù)s的為空的情況返回結(jié)果。當(dāng)p的第二個字符為*號時,由于*號前面的字符的個數(shù)可以任意,可以為0,那么我們先用遞歸來調(diào)用為0的情況,就是直接把這兩個字符去掉再比較,或者當(dāng)s不為空,且第一個字符和p的第一個字符相同時,再對去掉首字符的s和p調(diào)用遞歸,注意p不能去掉首字符,因為*號前面的字符可以有無限個;如果第二個字符不為*號,那么就老老實實的比較第一個字符,然后對后面的字符串調(diào)用遞歸,參見代碼如下:

解法二:

class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.empty()) return s.empty();
        if (p.size() > 1 && p[1] == "*") {
            return isMatch(s, p.substr(2)) || (!s.empty() && (s[0] == p[0] || p[0] == ".") && isMatch(s.substr(1), p));
        } else {
            return !s.empty() && (s[0] == p[0] || p[0] == ".") && isMatch(s.substr(1), p.substr(1));
        }
    }
};

我們也可以用 DP 來解,定義一個二維的 DP 數(shù)組,其中 dp[i][j] 表示 s[0,i) 和 p[0,j) 是否 match,然后有下面三種情況(下面部分摘自這個帖子):

1.  P[i][j] = P[i - 1][j - 1], if p[j - 1] != "*" && (s[i - 1] == p[j - 1] || p[j - 1] == ".");
2.  P[i][j] = P[i][j - 2], if p[j - 1] == "*" and the pattern repeats for 0 times;
3.  P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == "."), if p[j - 1] == "*" and the pattern repeats for at least 1 times.

解法三:

class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
        dp[0][0] = true;
        for (int i = 0; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (j > 1 && p[j - 1] == "*") {
                    dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == ".") && dp[i - 1][j]);
                } else {
                    dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == ".");
                }
            }
        }
        return dp[m][n];
    }
};

以上就是關(guān)于“C++怎么實現(xiàn)正則表達(dá)式匹配”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI