C++實現(xiàn)LeetCode(10.正則表達(dá)式匹配)
[LeetCode] 10. 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)LeetCode(10.正則表達(dá)式匹配)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)正則表達(dá)式匹配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++初學(xué)者之根據(jù)輸入的任何一個正整數(shù),輸出可能被表示的連續(xù)正整數(shù)
這篇文章主要介紹了C++初學(xué)者之根據(jù)輸入的任何一個正整數(shù),輸出可能被表示的連續(xù)正整數(shù)的相關(guān)資料,需要的朋友可以參考下2016-03-03Qt使用QCamera實現(xiàn)切換相機(jī),分辨率和圖像捕獲功能
這篇文章主要為大家介紹了如何利用Qt中的相機(jī)類QCamera,取景器類QCameraViewfinder,圖像捕獲類QCameraImageCapture實現(xiàn)切換相機(jī)、分辨率和圖像捕獲功能,需要的可以了解一下2023-04-04