53,53kf

來源:互聯網
上載者:User

53,53kf

題目:

請實現一個函數用來匹配包含‘.’和‘*’的Regex。模式中的字元’.’表示任意一個字元,而‘*’表示它前面的字元可以出現任意次(含0次)。本題中,匹配是指字串的所有字元匹配整個模式。例如,字串“aaa”與模式“a.a”和“ab*ac*a”匹配,但與“aa.a”及“ab*a”均不匹配。

解析:
字串 str = “aaa”; 模式字串 pattern = “.b*ac*a”
每次分別在str 和pattern中取一個字元進行匹配,如果匹配,則匹配下一個字元,否則,返回不匹配。
設匹配遞迴函式 match(str, pattern)。

如果模式比對字元的下一個字元是‘*’:

  • 如果pttern當前字元和str的當前字元匹配,:有以下三種可能情況
    • pttern當前字元能匹配 str 中的 0 個字元:match(str, pattern+2)
    • pttern當前字元能匹配 str 中的 1 個字元:match(str+1, pattern+2)
    • pttern當前字元能匹配 str 中的 多 個字元:match(str+1, pattern)
  • 如果pttern當前字元和和str的當前字元不匹配
    • pttern當前字元能匹配 str 中的 0 個字元:(str, pattern+2)

如果模式比對字元的下一個字元不是‘*’,進行逐字元匹配。

對於 ‘.’ 的情況比較簡單,’.’ 和一個字元匹配 match(str+1, pattern+1)
另外需要注意的是:Null 字元串”” 和 “.*” 是匹配的

bool MatchCore(const char* str, const char* pattern) {    if (*str == '\0' && *pattern == '\0')        return true;    // if (*str == '\0' && *pattern != '\0') return false : 不成立,如str = "", pattern=".*"    if (*str != '\0' && *pattern == '\0' )        return false;    if (*(pattern+1) == '*') {        if (*pattern == *str || *pattern == '.' && *str != '\0') {            //三種情況:*之前的字元出現 0 次, 出現一次,出現多次. pattern+2表示跳過當前字元和‘*’            return MatchCore(str, pattern+2) || MatchCore(str+1, pattern+2) || MatchCore(str+1, pattern);        } else {            // 沒有匹配,出現 0 次(包括str=“”,pattern=“.*”)            return MatchCore(str, pattern+2);        }    }    if (*str == *pattern || *pattern == '.' && *str != '\0')        return MatchCore(str+1, pattern+1);    return false;}bool Match(const char* str, const char* pattern) {    if (pattern == NULL || str == NULL)        return false;    return MatchCore(str, pattern);}

測試案例:
From:劍指offer源碼 GitHub

// ==================== Test Code ====================void Test(char* testName, char* string, char* pattern, bool expected){    if(testName != NULL)        printf("%s begins: ", testName);    if(Match(string, pattern) == expected)        printf("Passed.\n");    else        printf("FAILED.\n");}int main(int argc, char* argv[]){    Test("Test01", "", "", true);    Test("Test02", "", ".*", true);    Test("Test03", "", ".", false);    Test("Test04", "", "c*", true);    Test("Test05", "a", ".*", true);    Test("Test06", "a", "a.", false);    Test("Test07", "a", "", false);    Test("Test08", "a", ".", true);    Test("Test09", "a", "ab*", true);    Test("Test10", "a", "ab*a", false);    Test("Test11", "aa", "aa", true);    Test("Test12", "aa", "a*", true);    Test("Test13", "aa", ".*", true);    Test("Test14", "aa", ".", false);    Test("Test15", "ab", ".*", true);    Test("Test16", "ab", ".*", true);    Test("Test17", "aaa", "aa*", true);    Test("Test18", "aaa", "aa.a", false);    Test("Test19", "aaa", "a.a", true);    Test("Test20", "aaa", ".a", false);    Test("Test21", "aaa", "a*a", true);    Test("Test22", "aaa", "ab*a", false);    Test("Test23", "aaa", "ab*ac*a", true);    Test("Test24", "aaa", "ab*a*c*a", true);    Test("Test25", "aaa", ".*", true);    Test("Test26", "aab", "c*a*b", true);    Test("Test27", "aaca", "ab*a*c*a", true);    Test("Test28", "aaba", "ab*a*c*a", false);    Test("Test29", "bbbba", ".*a*a", true);    Test("Test30", "bcbbabab", ".*a*a", false);    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.