★leetcode10_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
注:这个题目还是需要认真审题的,'c*'是一体的,代表0个或多个c;'.*'则代表0个或多个任意字符。

二.代码编写

这一题需要考虑的情况很多,我在自行设计算法的时候没考虑清楚,提交的时候跳出来的特殊情况导致了我的整个算法都得推翻重来。其中,'.*'的匹配情况完全没办法通过打补丁来解决。

多次推导重来之后再网上搜索了该题的解法。解法基本上就是递归和dp两种思想,但是递归解法会导致TLE。


三.算法思想

递归思想:参考http://pengbos.com/algorithms/leetcode-regular-expression-matching-solution

String match, and it is a typical DP problem.

create a two dimension table dp[s.length()+1][p.length()+1]

so dp[i][j] means whether string [0, i-1] matches pattern [0, j-1]
and dp[i+1][j+1] means whether string [0, i] matches pattern [0, j]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值