1. #include"iostream"
  2. using namespace std;
  3.  
  4. bool MatchCore(char*str,char* pattern);
  5.  
  6. bool Match(char* str,char* pattern)
  7. {
  8. if(str==nullptr||pattern==nullptr)
  9. return false;
  10. return MatchCore(str,pattern);
  11. }
  12.  
  13. bool MatchCore(char*str,char* pattern)
  14. {
  15. if(*str=='\0'&&*pattern=='\0')
  16. return true;
  17. if(*(pattern+)=='*')
  18. {
  19. if(*str==*pattern||(*pattern=='.'&&*str!='\0'))
  20. return MatchCore(str,pattern+)||MatchCore(str+,pattern+) || MatchCore(str+,pattern);
  21. else
  22. return MatchCore(str,pattern+);
  23. }
  24. if(*str==*pattern||(*pattern=='.'&&*str!='\0'))
  25. return MatchCore(str+,pattern+);
  26. return false;
  27. }
  28.  
  29. void Test(char* testName,char* str,char* pattern,bool expect)
  30. {
  31. cout<<testName<<": ";
  32. if(Match(str,pattern)==expect)
  33. cout<<"passed."<<endl;
  34. else
  35. cout<<"failed."<<endl;
  36. }
  37.  
  38. int main()
  39. {
  40. Test("test1","aabcaa","a*b*.a*",true);
  41. Test("test2","","",true);
  42. Test("test3","aaa","a*a.",true);
  43. Test("test4",nullptr,nullptr,false);
  44. Test("test5","abccdde","b*ab*c*d*e.",false);
  45.  
  46. return ;
  47. }

官方答案,测试用例写的真是很详细:

  1. // 面试题19:正则表达式匹配
  2. // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式。模式中的字符'.'
  3. // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题
  4. // 中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"
  5. // 和"ab*ac*a"匹配,但与"aa.a"及"ab*a"均不匹配。
  6.  
  7. #include <cstdio>
  8.  
  9. bool matchCore(const char* str, const char* pattern);
  10.  
  11. bool match(const char* str, const char* pattern)
  12. {
  13. if(str == nullptr || pattern == nullptr)
  14. return false;
  15.  
  16. return matchCore(str, pattern);
  17. }
  18.  
  19. bool matchCore(const char* str, const char* pattern)
  20. {
  21. if(*str == '\0' && *pattern == '\0')
  22. return true;
  23.  
  24. if(*str != '\0' && *pattern == '\0')
  25. return false;
  26.  
  27. if(*(pattern + ) == '*')
  28. {
  29. if(*pattern == *str || (*pattern == '.' && *str != '\0'))
  30. // 进入有限状态机的下一个状态
  31. return matchCore(str + , pattern + )
  32. // 继续留在有限状态机的当前状态
  33. || matchCore(str + , pattern)
  34. // 略过一个'*'
  35. || matchCore(str, pattern + );
  36. else
  37. // 略过一个'*'
  38. return matchCore(str, pattern + );
  39. }
  40.  
  41. if(*str == *pattern || (*pattern == '.' && *str != '\0'))
  42. return matchCore(str + , pattern + );
  43.  
  44. return false;
  45. }
  46.  
  47. // ====================测试代码====================
  48. void Test(const char* testName, const char* string, const char* pattern, bool expected)
  49. {
  50. if(testName != nullptr)
  51. printf("%s begins: ", testName);
  52.  
  53. if(match(string, pattern) == expected)
  54. printf("Passed.\n");
  55. else
  56. printf("FAILED.\n");
  57. }
  58.  
  59. int main(int argc, char* argv[])
  60. {
  61. Test("Test01", "", "", true);
  62. Test("Test02", "", ".*", true);
  63. Test("Test03", "", ".", false);
  64. Test("Test04", "", "c*", true);
  65. Test("Test05", "a", ".*", true);
  66. Test("Test06", "a", "a.", false);
  67. Test("Test07", "a", "", false);
  68. Test("Test08", "a", ".", true);
  69. Test("Test09", "a", "ab*", true);
  70. Test("Test10", "a", "ab*a", false);
  71. Test("Test11", "aa", "aa", true);
  72. Test("Test12", "aa", "a*", true);
  73. Test("Test13", "aa", ".*", true);
  74. Test("Test14", "aa", ".", false);
  75. Test("Test15", "ab", ".*", true);
  76. Test("Test16", "ab", ".*", true);
  77. Test("Test17", "aaa", "aa*", true);
  78. Test("Test18", "aaa", "aa.a", false);
  79. Test("Test19", "aaa", "a.a", true);
  80. Test("Test20", "aaa", ".a", false);
  81. Test("Test21", "aaa", "a*a", true);
  82. Test("Test22", "aaa", "ab*a", false);
  83. Test("Test23", "aaa", "ab*ac*a", true);
  84. Test("Test24", "aaa", "ab*a*c*a", true);
  85. Test("Test25", "aaa", ".*", true);
  86. Test("Test26", "aab", "c*a*b", true);
  87. Test("Test27", "aaca", "ab*a*c*a", true);
  88. Test("Test28", "aaba", "ab*a*c*a", false);
  89. Test("Test29", "bbbba", ".*a*a", true);
  90. Test("Test30", "bcbbabab", ".*a*a", false);
  91.  
  92. return ;
  93. }

剑指offer——面试题19:正则表达式匹配的更多相关文章

  1. 【剑指Offer】52、正则表达式匹配

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

  2. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  3. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  4. 剑指offer面试题19 二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像.  输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

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

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

  6. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  7. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  8. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  9. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

随机推荐

  1. 实践作业3:白盒测试----junit的难点DAY11.

    本次白盒测试 需要独立完成整个项目和工具的配置安装运行操作,并编写.运行测试脚本,并完成实验的一些小细节等等. 首先,导入Junit测试框架所需的Jar包 然后编写测试脚本,为.java运行程序,见打 ...

  2. MySQL 存储过程 -流程控制的使用

    #五.流程控制的使用 #1.IF 使用 create PROCEDURE iftest1() BEGIN DECLARE a int DEFAULT 10; -- IF (a>1 &&a ...

  3. 回归(regression)、梯度下降(gradient descent)

    本文由LeftNotEasy所有,发布于http://leftnoteasy.cnblogs.com.如果转载,请注明出处,在未经作者同意下将本文用于商业用途,将追究其法律责任. 前言: 上次写过一篇 ...

  4. [转][译] 存储引擎原理:LSM

    原译文地址:http://www.tuicool.com/articles/qqQV7za http://www.zhihu.com/question/19887265 http://blog.csd ...

  5. WCF服务编程 读书笔记——第1章 WCF基础(1)

    第1章 WCF基础 本章主要介绍WCF的基本概念.构建模块以及WCF体系架构,以指导读者构建一个简单的WCF服务.从本章的内容中,我们可以了解到WCF的基本术语,包括地址(Address).绑定(Bi ...

  6. 在ie6下将png24图片透明

    没想到IETester中IE6和IE6真实版本不一样...之前一直没有实现png图片的透明度,现在发现原来是版本不一样惹的祸.总之,我将解决方法以demo的方式显示出来,以供再次利用. <!DO ...

  7. vmware之VMware Remote Console (VMRC) SDK(一)

    通过console可以实现类似远程桌面的功能,但它的实现方式和远程桌面不同,一般来说远程桌面必须要有网络支持,在机器关闭或者启动过程中无法连接.而console是通过esx的虚拟化组件实现远程桌面.在 ...

  8. C#连接MySql数据库代码

    之前学JAVA的时候,老师讲数据库的时候,讲到可以用一个类来连接数据库,叫做Dao层,今天要用C#做上位机,也有一些数据要写到数据库中去,我就想,能不能也给C#写一个这样的Dao层来连接数据库,我就去 ...

  9. Spring Boot - 记录日志

    比自己写文本日志的好处 默认定义好了一些日志级别,会记录当前使用的级别以上的日志,通常线上环境设置的级别较高记得较少 有一些自动split之类的功能 Commons-logging 日志级别:TRAC ...

  10. AJAX方式调用百度天气

    后台代码: [HttpPost] public string AjaxWeather() { string CityName = string.IsNullOrEmpty(Request.Form[& ...