zoukankan      html  css  js  c++  java
  • 《剑指offer》第十九题(正则表达式匹配)

    // 面试题19:正则表达式匹配
    // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式。模式中的字符'.'
    // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题
    // 中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"
    // 和"ab*ac*a"匹配,但与"aa.a"及"ab*a"均不匹配。
    
    #include <iostream>
    using namespace std;
    
    bool matchCore(const char* str, const char* pattern);
    
    bool match(const char* str, const char* pattern)
    {
        if (str == nullptr || pattern == nullptr)
            return false;
    
        return matchCore(str, pattern);
    }
    
    bool matchCore(const char* str, const char* pattern)
    {
        if (*str == '' && *pattern == '')//我去,还有这样的,这样的案例我老是想不到
            return true;
    
        if (*str != '' && *pattern == '')
            return false;
    
        if (*(pattern + 1) == '*')//当第二个是*
        {
            if (*pattern == *str || (*pattern == '.' && *str != ''))////首字母一样,进入有限状态机的下一个状态
                return matchCore(str + 1, pattern + 2)//可能重复一次
                || matchCore(str + 1, pattern) //可能不止重复一次
                || matchCore(str, pattern + 2);//可能一次都没有,略过一个'*'
            else
                //说明第一个字母不匹配,必须略过一个'*'
                return matchCore(str, pattern + 2);
        }
    
        if (*str == *pattern || (*pattern == '.' && *str != ''))//当第二个不是*
            return matchCore(str + 1, pattern + 1);
    
        return false;
    }
    
    // ====================测试代码====================
    void Test(const char* testName, const char* string, const char* pattern, bool expected)
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        if (match(string, pattern) == expected)
            printf("Passed.
    ");
        else
            printf("FAILED.
    ");
    }
    
    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);
        system("pause");
        return 0;
    }
  • 相关阅读:
    ASP.NET 4.0 与 Entity Framework 4第四篇Entity Framework在三层架构中的使用
    ASP.NET 4.0 与 Entity Framework 4第一篇采用ModelFirst 开发方式创建数据库
    ASP.NET 4.0 与 Entity Framework 4第二篇使用Entity Framework 进行CRUD操作
    ASP.NET 4.0 与 Entity Framework 4第三篇使用Entity Framework调用存储过程
    Unity3D4.* NGUI制作动态字库
    Unity3D内存释放
    1112 Stucked Keyboard (20 分)(map)【回顾】
    1116 Come on! Let's C (20 分)(hash散列)
    1108 Finding Average (20 分)(字符串处理)
    1113 Integer Set Partition (25 分)(排序)
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10486109.html
Copyright © 2011-2022 走看看