刷题10. Regular Expression Matching

一、题目说明

这个题目是10. Regular Expression Matching,乍一看不是很难。

但我实现提交后,总是报错。不得已查看了答案。

二、我的做法

我的实现,最大的问题在于对.*的处理有问题,始终无法成功。

#include<iostream>

using namespace std;

class Solution{
    public:
    bool isMatch(string s,string p){
        bool result = true;
        if(s.length()<=0 && p.length()<=0){
            return true;
        }
        if(p==".*"){
            return true;
        }

        int sCurr=0,pCurr=0;
        int lenS = s.length();
        int lenP = p.length();

        //count the num of .*
        int numOfWildCard = 0;
        while(pCurr<lenP){
            if(p[pCurr]=='.' && pCurr+1<lenP && p[pCurr+1]=='*'){
                numOfWildCard++;
            }

            pCurr++;
        }
        //cout<<numOfWildCard<<":";

        pCurr = 0;
        while(sCurr<lenS && pCurr<lenP){
            if((pCurr+1<lenP) && p[pCurr]=='.' && p[pCurr+1]=='*'){
                if(pCurr+2<lenP){
                    pCurr = pCurr+2;
                    while(sCurr<lenS && s[sCurr]!=p[pCurr]){
                        sCurr++;
                    }
                }

            }
            if((pCurr+1<lenP) && p[pCurr+1]=='*'){
                while(sCurr<lenS && s[sCurr]==p[pCurr]){
                    sCurr++;
                }
                pCurr = pCurr+2;
            }
            if(sCurr<lenS && pCurr<lenP && p[pCurr+1]!='*'){
                if(s[sCurr]==p[pCurr] || p[pCurr]=='.'){
                    sCurr++;
                    pCurr++;
                }
            }
        }

        if(sCurr==lenS && pCurr==lenP){
            result = true;
        }else{
            result = false;
        }
        return result;
    }
};

int main(){
    Solution s;
    cout<<(false==s.isMatch("aa","a"))<<endl;
    cout<<(true==s.isMatch("aa","a*"))<<endl;
    cout<<(true==s.isMatch("ab",".*"))<<endl;
    cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
    cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
    return 0;
}

三、正确的做法

1.递归方法

#include<iostream>
#include<vector>

using namespace std;

class Solution{
    public:
    bool isMatch(string s, string p) {//aa a
        if(p.empty()) return s.empty();
        if(s.empty()) return p.empty() || (p[1] == '*' ? isMatch(s, p.substr(2)) : false);
        if(p[0] != '.' && s[0] != p[0]) return p[1] == '*' ? isMatch(s, p.substr(2)) : false;
        if(p[1] == '*') return isMatch(s.substr(1), p) || isMatch(s, p.substr(2));
        return isMatch(s.substr(1), p.substr(1));
    }
};

int main(){
    Solution s;
    cout<<(false==s.isMatch("aa","a"))<<endl;
    cout<<(true==s.isMatch("aa","a*"))<<endl;
    cout<<(true==s.isMatch("ab",".*"))<<endl;
    cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
    cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
    cout<<(false==s.isMatch("ab",".*c"))<<endl;
    cout<<(true==s.isMatch("aaa","a*a"))<<endl;
    return 0;
}

2.DP方法

dp是什么?动态规划啊,

#include<iostream>
#include<vector>
#include <mem.h>

using namespace std;

class Solution {
public:
    bool isMatch(string s, string p) {
        int ssize = s.size(),psize = p.size();
        string pp="";
        vector<bool> star;

        for(int i=0;i<p.size();i++){
            if(p[i]=='*'){
                star.back()=true;
            }else{
                star.push_back(false);
                pp+= p[i];
            }
        }

        psize = pp.size();

        bool dp[psize+1][ssize+1];
        memset(dp,false,sizeof(dp));

        dp[0][0] = true;
        for(int i=1;i<=psize;i++){
            if(star[i-1]==true){
                dp[i][0] = true;
            }else{
                break;
            }
        }

        for(int i=1;i<=psize;i++)
            for(int j=1;j<=ssize;j++){
                if(dp[i-1][j-1]== true){
                    if(pp[i-1]==s[j-1] || pp[i-1]=='.'){
                        dp[i][j] = true;
                        continue;
                    }
                }

                if(dp[i-1][j]== true){
                    if(star[i-1]==true){
                        dp[i][j] = true;
                        continue;
                    }
                }

                if(dp[i][j-1]== true){
                    if(star[i-1]==true && (pp[i-1]==s[j-1] || pp[i-1]=='.')){
                        dp[i][j] = true;
                        continue;
                    }
                }
            }
        return dp[psize][ssize];
    }
};

int main(){
    Solution s;
    cout<<(false==s.isMatch("aa","a"))<<endl;
    cout<<(true==s.isMatch("aa","a*"))<<endl;
    cout<<(true==s.isMatch("ab",".*"))<<endl;
    cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
    cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
    cout<<(false==s.isMatch("ab",".*c"))<<endl;
    cout<<(true==s.isMatch("aaa","a*a"))<<endl;
    cout<<(false==s.isMatch("a",""))<<endl;
    return 0;
}

四、总结

看来基础知识还需要恶补,加油!

原文地址:https://www.cnblogs.com/siweihz/p/12233106.html

时间: 2024-10-27 02:39:09

刷题10. Regular Expression Matching的相关文章

10. Regular Expression Matching &amp;&amp; 44. Wildcard Matching

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).

(待解决)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

10. Regular Expression Matching(hard)

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

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

10. Regular Expression Matching字符串.*匹配

[抄题]: 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 entire input str

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

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

【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. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串

[eetcode 10]Regular Expression Matching

1 题目: 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 sho