leetcode-10 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

看着题目就难懂,找规律都有些复杂。纠结了几天,还是想不出好的方法,只得去discuss学习学习,然后百度了一下,发现大神太多,水货怎么活………
下面是借鉴过来的,别人的分析与思路,必须好好学习!感谢大神的分享!!
http://blog.csdn.net/hopeztm/article/details/7992253
这是一道动态规划题。
题目意图很简单,就是实现一个正则表达式匹配的判定函数。 特别要说一下的是 .* 这个格式,因为不是经常用,最开始想错了,以为是先匹配”.” 然后 “.” 匹配成什么,后面的 * 就扩展什么。

其实核心的思路是一个动态规划

dp[i][j]表示字串 s[i…len(s)], p[j…len(p)] 是否可以匹配。

那么状态转移方程如下:

dp[i][j] =

c1. p[j+1] != *. if s[i] == p[j] dp[i][j] = dp[i+1][j+1]

else dp[i][j] = false

c2 p[j+1] == ‘’ (这个情况下,要扩展 , dp[i][j] 从拓展的情况下,选择一个是真的结果)

if( s[i] == p[j] || p[j] == ‘.’ && (*s) != 0) 当s[i] 和 p[j] 一样的时候,例如 aba, a*b这个时候,i = 0, j = 0, 自然可以匹配a a
如果p[j] == . 因为他可以匹配任何字符,所以和相等关系有基本一样的方式。
并且每一步匹配都要递增 i 的值,如果有成立的,则返回true,否则到匹配终了,返回通配符匹配完成后的结果。

别人写的C代码

bool isMatch(char* s, char* p) {
    if (s == NULL || p == NULL) return false;
    if (*p == '\0') return *s == '\0';
    // ".*" matches "", so we can't check (*s == '\0') here.

    if (*(p + 1) == '*')
    {
        // Here *p != '\0', so this condition equals with
        // (*s != '\0' && (*p == '.' || *s == *p)).
        while ((*s != '\0' && *p == '.') || *s == *p)
        {
            if (isMatch(s, p + 2)) return true;
            ++s;
        }

        return isMatch(s, p + 2);
    }
    else if ((*s != '\0' && *p == '.') || *s == *p)
    {
        return isMatch(s + 1, p + 1);
    }

    return false;
}

还有其他大神的代码,分别用的是递归和动态规划。
其中递归的代码与上面的方法一样。动态规划则不同。看起来,递归的代码简洁,思路清晰。动态规划的代码,比较繁琐。不过明显,动态规划的代码运行速度快,递归的由于嵌套所以较慢。算是各有所长。
https://leetcode.com/discuss/28725/c-recursive-solution-and-dp-solution

recursive solution:

bool isMatch(const char *s, const char *p) {
    if(!*p) return !*s;
    if(!*s) return *(p+1)=='*' && isMatch(s, p+2);
    if(*(p+1)=='*')
        return isMatch(s, p+2) || (*s==*p || *p=='.') && isMatch(s+1, p);
    else
        return (*s==*p || *p=='.') && isMatch(s+1, p+1);
}
DP solution:

bool isMatch(const char *s, const char *p) {
    const int m=strlen(s), n=strlen(p);
    bool arr[m+1][n+1];
    fill_n(*arr, (m+1)*(n+1), false);
    arr[0][0]=true;
    for(int j=2; j<=n; j+=2)
        if(arr[0][j-2] && p[j-1]=='*')
            arr[0][j]=true;
        else
            break;
    for(int i=1; i<=m; ++i)
        for(int j=1; j<=n; ++j)
            if(p[j-1]=='*')
                arr[i][j]=arr[i][j-2] || arr[i-1][j] && (s[i-1]==p[j-2] || p[j-2]=='.');
            else
                arr[i][j]=arr[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
    return arr[m][n];
}

路漫漫其修远兮啊……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值