博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode] Regular Expression Matching
阅读量:6240 次
发布时间:2019-06-22

本文共 2601 字,大约阅读时间需要 8 分钟。

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true

 

注意:这里的a*表示a可以重复0次或者多次,不是a和*分开的。

 It seems that some readers are confused about why the regex pattern ".*" matches the string"ab"".*" means repeat the preceding element 0 or more times. Here, the "preceding" element is the dot character in the pattern, which can match any characters. Therefore, the regex pattern".*" allows the dot to be repeated any number of times, which matches any string (even an empty string). Think carefully how you would do matching of '*'.Please note that '*' in regular expression is different from wildcard matching, as we match the previous character 0 or more times. But, how many times? If you are stuck,recursion is your friend.

 动态规划,无敌存在

class Solution {public:    bool isMatch(const char *s, const char *p) {        int i, j;        int m = strlen(s);        int n = strlen(p);        /**         * b[i + 1][j + 1]: if s[0..i] matches p[0..j]         * if p[j] != '*'         * b[i + 1][j + 1] = b[i][j] && s[i] == p[j]         * if p[j] == '*', denote p[j - 1] with x,         * then b[i + 1][j + 1] is true iff any of the following is true         * 1) "x*" repeats 0 time and matches empty: b[i + 1][j -1]         * 2) "x*" repeats 1 time and matches x: b[i + 1][j]         * 3) "x*" repeats >= 2 times and matches "x*x": s[i] == x && b[i][j + 1]         * '.' matches any single character         */        bool b[m + 1][n + 1];        b[0][0] = true;        for (i = 0; i < m; i++) {            b[i + 1][0] = false;        }        // p[0..j - 2, j - 1, j] matches empty iff p[j] is '*' and p[0..j - 2] matches empty        for (j = 0; j < n; j++) {            b[0][j + 1] = j > 0 && '*' == p[j] && b[0][j - 1];        }        for (i = 0; i < m; i++) {            for (j = 0; j < n; j++) {                if (p[j] != '*') {                    b[i + 1][j + 1] = b[i][j] && ('.' == p[j] || s[i] == p[j]);                } else {                    b[i + 1][j + 1] = b[i + 1][j - 1] && j > 0 || b[i + 1][j] ||                                b[i][j + 1] && j > 0 && ('.' == p[j - 1] || s[i] == p[j - 1]);                }            }        }        return b[m][n];    }};

 

转载地址:http://jjdia.baihongyu.com/

你可能感兴趣的文章
python2 与python3中最大的区别(编码问题bytes&str
查看>>
HDU 2243 AC自动机+DP+矩阵
查看>>
什么叫脱字符合^
查看>>
git版本控制管理实践-2
查看>>
HTTP基础知识(三)
查看>>
如何有效释放DB2所占的磁盘空间?
查看>>
三分法
查看>>
第 8 章 容器网络 - 058 - flannel 概述
查看>>
Mongodb删除collection
查看>>
ArcEngine应用程序中无法实现TOC图层多选
查看>>
Java-笔记9-复习
查看>>
python---基本数据结构
查看>>
Windows下JDK,Tomcat,Eclipse安装配置
查看>>
vue的checkbox或多选的select的代码例子
查看>>
es6-Set和Map数据结构
查看>>
使用ffmpeg将录屏文件转换成gif
查看>>
作业七 总结
查看>>
Oracle的静默安装 升级和卸载 参考规范
查看>>
高效存储过程分页
查看>>
电脑用U盘启动
查看>>