剑指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 ...
随机推荐
- Java 读取jar内的文件的超简便方法
坑爹的java课程设计,偏要用jar来运行 读取.存储jar内文件的支持也好低 存储方法: 进入jar文件其实没有说的那么困难,jar文件本质是一个zip格式的压缩文件,只是把文件后缀名改了,要用Ja ...
- ubuntu启动流程和要读取相关文件
当前系统环境为:linux mint mate 17.1(基于ubuntu14.04的衍生版) 查阅资料后总结如下: 首先: /etc/rc.d链接目标为:/etc /etc/rc*.d文件夹中的脚本 ...
- SSH2免密码登录OpenSSH
OpenSSH免密码登录SSH2http://blog.csdn.net/aquester/article/details/23836245 两个SSH2间免密码登录http://blog.csdn. ...
- iOS应用开发之CoreData[转]
我目前的理解,CoreData相当于一个综合的数据库管理库,它支持sqlite,二进制存储文件两种形式的数据存储.而CoreData提供了存储管理,包括查询.插入. 删除.更新.回滚.会话管理.锁管理 ...
- 设计模式12: Proxy 代理模式(结构型模式)
Proxy 代理模式(结构型模式) 直接与间接 人们对于复杂的软件系统常常有一种处理手法,即增加一层间接层,从而对系统获得一种更为灵活.满足特定需求的解决方案.如下图,开始时,A需要和B进行3次通信, ...
- 检测远程主机上的某个端口是否开启——telnet命令
要测试远程主机上的某个端口是否开启,无需使用太复杂的工作,windows下就自带了工具,那就是telnet.ping命令是不能检测端口,只能检测你和相应IP是否能连通. 1 安装telnet.win7 ...
- Vue--axios:vue中的ajax异步请求(发送和请求数据)、vue-resource异步请求和跨域
跨域原理: 一.使用axios发送get请求 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 & ...
- WPF 简洁的主界面
用的是dev的TileLayouotControl控件. <dxwui:PageAdornerControl Header="" Padding="30" ...
- Webserver asp配置及伪静态设置
Webserver IIS asp配置及伪静态设置 一.概述: 在Windows Server 2003系统中,用户可以借助IIS 6.0配置基于ASP.PHP.asp.NET等语言的动态Web网站 ...
- Windows 64位操作系统的ODBC
我的操作系统是windows server 2008 R2 X64,系统自带两个版本的ODBC管理器,在"运行"中输入下面内容分别调出他们: X64: C:\windows\sys ...