# -*- coding: utf8 -*-
'''
https://oj.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 ===Comments by Dabay===
这道题应该用动态规划的做法。我只想说,头大! 建立一个二维数组,第一维是s为基础,第二维是p为基础。(顺序很重要)
初始化res[0],长度比p多1,即多第一个true。同时考虑p中开始的几个偶数为是*的情况,初始化相应的true。其他为false。 然后每次放一个s中元素进来,尝试匹配整个p。 三种情况:(每次更新的是res[i+1][j+1])
如果p[j]不是.和*,即是一般比较,同时观察res[i][j]。
如果p[j]是.,无需比较,根据res[i][j]的值即可。
如果p[j]是*,
考虑匹配零次的情况:判断res[i+1][j-1]是否为true
考虑匹配一次的情况:判断res[i+1][j]是否为true
考虑匹配多次的情况:判断字符是否相符,同时判断res[i][j+1]
'''
class Solution:
# @return a boolean
def isMatch(self, s, p):
res = []
defaultRow = [False] + [False for _ in p]
res.append(list(defaultRow))
res[0][0] = True
for x in xrange(len(p)):
if p[x] == '*':
res[0][x+1] = res[0][x-1] for i in xrange(len(s)):
res.append(list(defaultRow))
for j in xrange(len(p)):
if p[j] != '*':
if (s[i] == p[j] or p[j] == '.') and res[i][j]:
res[i+1][j+1] = True
elif p[j] == '.':
if res[i][j]:
res[i+1][j+1] = True
else: # *
#匹配0次
if res[i+1][j-1]:
res[i+1][j+1] = True
#匹配1次
if res[i+1][j]:
res[i+1][j+1] = True
#匹配多次
if (s[i] == p[j-1] or p[j-1] == '.') and res[i][j+1]:
res[i+1][j+1] = True
return res[-1][-1] def main():
sol = Solution()
s = "aab"
p = "c*a*b"
print sol.isMatch(s, p) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python][DP]Regular Expression Matching的更多相关文章

  1. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  4. LeetCode解题报告—— Regular Expression Matching

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  7. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  9. [leetcode]Regular Expression Matching @ Python

    原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...

随机推荐

  1. IIS7.0 部署wcf 404或者配置MIME(转)

    WCF部署在IIS下,报错如下: 应用程序“DEFAULT WEB SITE/IMF”中的服务器错误 Internet 信息服务 7.0 错误摘要 HTTP 错误 404.3 - Not Found由 ...

  2. RSA—非对称加密算法

    RSA:非对称加密算法加解密原理如下:已知:p,q,n,e,d,m,c其中:p与q互为大质数,n=p*q 公钥Pk(n,e):加密使用,是公开的 私钥Sk(n,d):解密使用,不公开 c:明文 m:密 ...

  3. MySQL----information-schema数据库相关权限的说明。

    MySQL中的information_schema数据库比较特别有如下几个要注意的地方. 1.就算是一个新创建的用户,也就是说这个用户只有一个usage权限.它都可以查看informatoin_sch ...

  4. bash快捷建-光标移到行首、行尾等

    转自:http://digdeeply.org/archives/12131599.html ctrl键组合ctrl+a:光标移到行首.ctrl+b:光标左移一个字母ctrl+c:杀死当前进程.ctr ...

  5. docker 网络4种模式

    1.host 模式,使用docker run 时 使用--net=host 指定 docker 使用的网络和宿主机一样,在容器上看到的网卡ip就是宿主机上的ip 2.container 模式,使用-- ...

  6. cdoj 秋实大哥与战争

    首先,显然每个区间的最长连续子区间要么在左孩子里,要么在右孩子里,要么跨越两个孩子.于是我们可以对每个区间维护如下信息ll(left long),rl(rigth long),ml(mid long) ...

  7. foj 2150 Fire Game(bfs暴力)

         Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...

  8. JS计算两个日期时间之差之天数不正确

    做了一个时间倒计时,发现天数总是不正确. js代码: //定义目标日期 var targetTime = new Date(); //目标日期 targetTime.setFullYear(2015, ...

  9. iOS 的 APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸?(转)

    原文:http://www.niaogebiji.com/article-4379-1.html?utm_source=tuicool 初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 ...

  10. C# NameValueCollection集合 .

    案例: NameValueCollection nameValueCollection = Request.Params;//获得连接地址中的所有参数 //获取各个参数,eg:            ...