剑指offer——面试题19:正则表达式匹配
#include"iostream"
using namespace std; bool MatchCore(char*str,char* pattern); bool Match(char* str,char* pattern)
{
if(str==nullptr||pattern==nullptr)
return false;
return MatchCore(str,pattern);
} bool MatchCore(char*str,char* pattern)
{
if(*str=='\0'&&*pattern=='\0')
return true;
if(*(pattern+)=='*')
{
if(*str==*pattern||(*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(char* testName,char* str,char* pattern,bool expect)
{
cout<<testName<<": ";
if(Match(str,pattern)==expect)
cout<<"passed."<<endl;
else
cout<<"failed."<<endl;
} int main()
{
Test("test1","aabcaa","a*b*.a*",true);
Test("test2","","",true);
Test("test3","aaa","a*a.",true);
Test("test4",nullptr,nullptr,false);
Test("test5","abccdde","b*ab*c*d*e.",false); return ;
}
官方答案,测试用例写的真是很详细:
// 面试题19:正则表达式匹配
// 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式。模式中的字符'.'
// 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题
// 中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"
// 和"ab*ac*a"匹配,但与"aa.a"及"ab*a"均不匹配。 #include <cstdio> 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); return ;
}
剑指offer——面试题19:正则表达式匹配的更多相关文章
- 【剑指Offer】52、正则表达式匹配
题目描述: 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹 ...
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 【剑指offer】面试题 19. 正则表达式匹配
面试题 19. 正则表达式匹配
- 剑指Offer——笔试题+知识点总结
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
随机推荐
- DataTable数据分页
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Refle ...
- 并发编程CAS操作
并发编程CAS操作 简介 CAS即compare and swap,中文就是比较并交换 CAS是Java并发包的基石 原理 其实CAS的原理相对来说比较简单.将要被改变的数据和期望的值作比较,当两个值 ...
- R Markdown 速查表
- sql查询层级分类
先上个效果图吧 CTE递归查询里面用了一些小的技巧,查询出结果以后在前端用表格展示出来,层级视觉效果还是很明显的 with tree as(select [ID],[Name],[Address],[ ...
- oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)
数据文件和日志文件是数据库中最重要的文件.它们是数据存储的地方.每个数据库至少有一个与之相关的数据文件,通常情况下不只一个,有很多.数据在数据文件中是如何组织的?要了解这些内容我们首先必须理解什么是表 ...
- delphi7列宽自定设置为固定值
- DELPHI XE5-8 弹出列表框供选择
点击章节练习: var tmplm:Tlistboxitem; begin dm.FDQTMP.SQL.Clear; dm.FDQTMP.SQL.Add('select GSESSON from TS ...
- 20145233《网络对抗》Exp7 DNS网络欺诈技术防范
20145233<网络对抗>Exp7 DNS网络欺诈技术防范 实验问题思考 通常在什么场景下容易受到DNS spoof攻击 公共的无线局域网中,容易受到攻击者的攻击,因为这样就会连入局域网 ...
- Windows10使用
1.常见问题 Win10 的操作中心如果不见了 Windows10电脑系统时间校准 实现windows与ubuntu的之间的复制与粘贴 安卓手机传递文件到Windows系统电脑 安卓手机高速传递文件到 ...
- Mysql内置功能《五》 函数
一 函数 MySQL中提供了许多内置函数,例如: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种子)使RAND( ...