leetcode题解||Regular Expression Matching 问题

problem:

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

'.' Matches any single character.
'*' Matches zero or more of the<span style="color:#ff0000;"> preceding element</span>.

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

thinking:

吐槽几句:

字符串匹配,‘.‘和‘*‘的意义很简单,不用陈述,但:

isMatch("aab", "c*a*b") → true

算哪门子事?

leetcode (http://articles.leetcode.com/2011/09/regular-expression-matching.html)已有好多人在争论,这C*是不是可以代表0个C,我TM无语了。

最后的感觉就是这道题为了宣传那个特定的算法而把条件改了?

PS: 看错题目了.....2B了

(1)没有*的情况很好解决,难在怎么处理*

(2)比如ABBC与 A*C、A*BC,很清楚,利用深搜的思想,只要把BB与*、*B整体作匹配就OK了

自己写了个测试程序,官方的那个扯淡条件通不过

#include <iostream>
#include <memory.h>
using namespace std;
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        int n=0;
        int m=0;
        int index=0;
        while(*(s+n)!='\0')
        {
            n++;
        }
        while(*(p+m)!='\0')
        {
            m++;
        }
       // cout<<"n: "<<n<<"m: "<<m<<endl;
        if(n==0||m==0)
            return false;
        int *a = new int[m];
        memset(a,0,sizeof(int)*m);
        if((*p=='.')||(*p==*s))
        {
            a[0]=1;
            index++;
        }
        for(int i=1;i<m;i++)
        {
            if(a[i]==1)
                continue;
            while(index<n)
            {
                if(*(p+i)=='.')
                {
                    a[i]=a[i-1]&0x01;
                    index++;
                }
                else if(*(p+i)=='*')
                {
                    int tmp_s=1;
                    int tmp_p=1;
                    while((*(s+index)==*(s+index+tmp_s))&&(index+tmp_s<n))
                    {
                        tmp_s++;
                    }
                    while((*(s+index)==*(p+i+tmp_p))&&(i+tmp_p<m))
                    {
                        tmp_p++;
                    }
                    if((index+tmp_s==n-1)&&(i+tmp_p==m-1))
                    {
                        a[i+tmp_p]=a[i-1]&0x01;
                        index+=tmp_s;
                        break;
                    }
                    else
                    {
                        for(int j=0;j<tmp_p;j++)
                        {
                            a[i+j]=a[i+j-1]&0x01;
                            index+=tmp_s;
                        }
                    }//else

                }//else if
                else
                {

                    if(*(s+index)==*(p+i))
                    {
                        index++;
                        a[i]=a[i-1]&0x01;
                       // cout<<"i: "<<i<<"a[i]:"<<a[i]<<endl;
                    }
                    else
                    {
                        a[i]=a[i-1]&0x00;
                        return false;

                    }

                }//else
                if(i==m-1)
                    break;
            }//while
        }//for
        if(index<n)
            return false;
        else
            return a[m-1];
        }//isMatch
    };

int main()
{
    char *s="aab";
    char *p="aa";
    Solution mysolution;
    cout<<mysolution.isMatch(s,p)<<endl;

}

我测试了几组,通过了,欢迎找BUG。

时间: 2024-10-13 21:11:07

leetcode题解||Regular Expression Matching 问题的相关文章

LeetCode 010 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 shou

LeetCode——010 Regular Expression Matching

title: LeetCode--010 Regular Expression Matching author: zzw top: false toc: true mathjax: false email: [email protected] date: 2020-02-19 22:05:32 updated: 2020-02-19 22:05:32 img: summary: categories: LeetCode tags: 字符匹配 --- Description Given an in

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

[LeetCode][Python]Regular Expression Matching

# -*- 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 matchin

LeetCode (10): Regular Expression Matching [HARD]

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 ent

[LeetCode][JavaScript]Regular Expression Matching

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

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 Given an input string (s) and a pattern (p), 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 entir

Java [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. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string