// 面试题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 == '\0' && *pattern == '\0')//我去,还有这样的,这样的案例我老是想不到
return true; if (*str != '\0' && *pattern == '\0')
return false; if (*(pattern + ) == '*')//当第二个是*
{
if (*pattern == *str || (*pattern == '.' && *str != '\0'))////首字母一样,进入有限状态机的下一个状态
return matchCore(str + , pattern + )//可能重复一次
|| matchCore(str + , pattern) //可能不止重复一次
|| matchCore(str, pattern + );//可能一次都没有,略过一个'*'
else
//说明第一个字母不匹配,必须略过一个'*'
return matchCore(str, pattern + );
} if (*str == *pattern || (*pattern == '.' && *str != '\0'))//当第二个不是*
return matchCore(str + , pattern + ); 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.\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);
system("pause");
return ;
}

《剑指offer》第十九题(正则表达式匹配)的更多相关文章

  1. 剑指offer五十二之正则表达式匹配

    一.题目 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式 ...

  2. 剑指Offer(十九):顺时针打印矩阵

    剑指Offer(十九):顺时针打印矩阵 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  3. 剑指offer(leetcode 10.) 正则表达式匹配

    这题一年前就做过,当时刚开始刷leetcode,提交了几十次过不去,就放那没管了.今天剑指offer又遇到这题,终于做出来了,用的dp. class Solution { public: bool i ...

  4. 剑指offer——面试题19:正则表达式匹配

    #include"iostream" using namespace std; bool MatchCore(char*str,char* pattern); bool Match ...

  5. 【剑指offer】面试题 19. 正则表达式匹配

    面试题 19. 正则表达式匹配

  6. 剑指Offer(十九)——顺时针打印矩阵

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 例如,如果输入如下4 X 4矩阵: 1   2    3     4 5   6    7     8 9   10  11  ...

  7. 剑指offer五十九之按之字形顺序打印二叉树

    一.题目 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.二.思路 详见代码 三.代码 import j ...

  8. 剑指offer四十九之把字符串转换成整数

    一.题目 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 二.思路 详见代码. 三.代码 public class Solution { ...

  9. 剑指offer三十九之平衡二叉树

    一.题目 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 二.思路 详解代码. 三.代码 public class Solution {     //判断根节点左右子树的深度,高度差超过1,则不平衡 ...

  10. 剑指offer二十九之最小的K个数

    一.题目 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.思路 详解代码. 三.代码 import java.util. ...

随机推荐

  1. jmeter bean shell断言加密的响应信息(加密接口测试二)

    断言加密的响应信息 1.在http请求-->添加-->断言-->bean shell 断言 import com.changfu.EncryptAndDecryptInterface ...

  2. type Props={};

    Components Learn how to type React class components and stateless functional components with Flow Se ...

  3. 9/24matplotlib简介

    Matplotlib是一个在python下实现的类matlib的纯python的三方库,旨在用python实现matlib的功能,是python下最出色的绘图库,功能很完善,其风格根matlib很相似 ...

  4. Leetcode: Binary Tree Level Order Transversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. 网页中自适应的显示PDF

    PDF格式呢,是一个高大的新式,如何在不同的浏览器中自适应显示,是一个值得研究的问题. 这里说明重点部分:获取浏览器宽高. IE中: document.body.clientWidth ==> ...

  6. uva12206 后缀数组

    这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用 利用后缀数组计算最长的lcp 这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 ...

  7. 微信分享缩略图,如何增加微信朋友圈分享链接的小图片? facebook、google+、twitter等分享的标签

    微信分享缩略图 如何增加微信朋友圈分享链接的小图片?在网页的头部<head>标签内加上以下代码,图片路径自行修改.<head><div id='wx_pic' style ...

  8. php CI框架实现验证码功能和增强验证码安全性实战教程

    php CI框架实现验证码功能和增强验证码安全性实战教程 CodeIgniter简称CI是最流行的一个php MVC框架之一,本人讲从实际项目使用中写系列实战经验,有别与其他的理论讲解文章,会附上实战 ...

  9. 解析分布式锁之Redis实现(二)

    摘要:在前文中提及了实现分布式锁目前有三种流行方案,分别为基于数据库.Redis.Zookeeper的方案,本文主要阐述基于Redis的分布式锁,分布式架构设计如今在企业中被大量的应用,而在不同的分布 ...

  10. 算法之路 level 01 problem set

    2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...