10. Regular Expression Matching
来源:互联网 发布:js砸金蛋源码下载 编辑:程序博客网 时间:2024/04/26 21:02
题目:Regular Expression Matching
原题链接:https://leetcode.com/problems/regular-expression-matching/
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”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “a*”) → true
isMatch(“aa”, “.*”) → true
isMatch(“ab”, “.*”) → true
isMatch(“aab”, “c*a*b”) → true
判断两个字符串是不是可以完全匹配起来。
规则如下:
’ . ‘可以代表任意一个字符,’ * ‘代表前面一个字符的个数是 0 个或者任意多个.
例:
isMatch(“aa”,”a”) → false 无法匹配
isMatch(“aa”,”aa”) → true 可以匹配
isMatch(“aaa”,”aa”) → false 无法匹配
isMatch(“aa”, “a*”) → true ’ * ‘可以解释成 a 的个数是2个,匹配成功
isMatch(“aa”, “.“) → true ’ . ‘可以表示 a ,’ ‘表示个数为2,匹配成功
isMatch(“ab”, “.“) → true “.“可以理解成’ . ‘的个数为2,那么可以匹配成”ab”,匹配成功
isMatch(“aab”, “c*a*b”) → true 后面的式子可以翻译成0个’ c ‘,2个’ a ‘和b,匹配成功
假设判断的两个字符串(s 和 p)的长度为row和col,设 bool 型 dp[ row + 1][ cow + 1]。
dp[ 0 ][ 0 ] = true,之后每一行的第一个元素设置为 false 。
dp[ i ][ j ]表示s[ 0…i - 1 ]和p[ 0…j - 1 ]的配对情况。
对于第一行即i == 0的时候,表示 s 的元素为空,所以只有当p[0…j - 1]能表示空的时候dp[ 0 ][ j ]才为true。首先 j == 1的时候肯定是false,因为第一个元素肯定是存在的,不可能表示为空。在j > 1的时候,只有在dp[ j - 2]为true,并且p[ j - 2]为任意,且p[ j - 1 ]为’ * ‘时才可以继续匹配为空。
综合起来表达式就是: dp[i][j] = j > 1 && (p[j - 1] == ‘*’) && dp[i][j - 2];
对于其他行,分两种情况讨论:
1. p[ j - 1]是’ * ‘号的时候,这个时候如果s[0…i - 1]和p[0…j - 2]配对成功,那么s[0…i - 1]和p[0…j - 1]也可以配对成功,或者s[0…i - 2]和p[0…j - 1]已经配对成功并且发生下面两种情况之一:a. s[ i - 1 ] 和p[ j - 1 ]相同 b. p[ j - 1 ]是’ . ‘。当发生以上两种情况的时候s[0…i - 1]和p[0…j - 1]也是可以配对成功的。
2. p[ j - 1 ]不是’ * ‘号,那么只有在s[0…i - 2]和p[0…j - 2 ]已经配对成功的情况下,接下来的字符也配对成功才能保证配对继续保持成功,这就只可能是要么两个字符一样,要么p[ j - 1]是’ . ‘才行。
对后dp[ row ][ col ]就是配对的结果。
代码如下:
class Solution {public: bool isMatch(string s, string p) { int row = s.length(), col = p.length(); bool dp[row + 1][col + 1]; dp[0][0] = true; for(int i = 1; i <= row; ++i) { dp[i][0] = false; } for(int i = 0; i <= row; ++i) { for(int j = 1; j <= col; ++j) { if(!i) { dp[i][j] = j > 1 && (p[j - 1] == '*') && dp[i][j - 2]; }else if(p[j - 1] == '*'){ dp[i][j] = (dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')) || dp[i][j - 2]; }else{ dp[i][j] = dp[i - 1][j - 1] && (p[j - 1] == '.' || p[j - 1] == s[i - 1]); } } } return dp[row][col]; }};
- 10. Regular Expression Matching
- 10.Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 【软件项目管理】软件项目的主要成本是人的劳动的消耗
- 轮播框的js写法
- 信道 | 信息论
- Linear Programming_the simplex method in tableau format
- PHP如何获取文件行数
- 10. Regular Expression Matching
- 设置mysql默认字符集为utf8
- 文本编辑器vim的基本操作
- 数据结构之Binary Heap(二叉堆)
- UIWindow :窗口 (UIView) UIView: 视图
- poj2528线段树+离散化
- 【c++】析构函数中delete this指针
- notepad++ 如何清除空格或EOL转换
- JAVASCRIPT:检查日期是否正确(正则表达式)