失败的尝试 10. regular expression matching & 正则

Regular Expression Matching

看到正则就感觉头大,因为正则用好了就很强大。有挑战的才有意思。

其实没有一点思路。循环的话,不能一一对比,匹配模式解释的是之前的字符。那就先遍历模式把。

... 中间 n 次失败的提交

感觉代码逻辑很乱。重新捋一下再动手写。

找几个重点分析一下:

Wrong Answer:

Input:
"aaa"
"ab*a*c*a"
Output:
false
Expected:
true

调试

aaa ab*a*c*a
0 a a s
1 a b n
1 a * * b
1 a a s
2 a * * a
prev char eq
False

分析,aaa字符串s中s[1]的a被模式p[3]中的a匹配了,然后s[2]的a被p[4]的*匹配了。还是没有解决*匹配0次的问题,那就得预先判断后面是啥模式而不是在之后判断前面的一个模式是啥。

N小时后来更,改了好多次没有解决匹配0-多次字符之后还有该字符。

可能我钻牛角尖了,删掉重新想一种思路。

... 又 n 次失败的本地测试
failed submission
import time

class Solution:
    def __init__(self):
        self.any=‘.‘ # any character
        self.zom=‘*‘ # zero or more
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        ci=0

        prevPattern=None

        for pi,pa in enumerate(p):
            if ci==len(s):
                if len(s)==0:
                    continue

                if ci>0 and prevPattern==self.zom:
                    ci-=1
                else:
                    break
                print("other:",pa)
                #continue
            while ci < len(s):
                ci+=1
                print(pi,pa,ci-1,s[ci-1],end="| ")
                if pa==self.any:
                    print(‘.‘)
                    break
                elif pa==self.zom:
                    print(‘*‘,prevPattern)
                    if prevPattern==self.any:
                        continue
                    elif prevPattern==s[ci-1]:
                        continue
                    else:
                        # no match, end processing
                        pass
                        #prevPattern=‘‘
                        ci-=1
                        break
                    break
                elif pa==s[ci-1]:
                    # same character
                    print(‘s‘)
                    break
                else:
                    print(‘n‘)
                    ci-=1
                    break
            prevPattern=pa
        else:
            return ci==len(s)

        return False

if __name__ == "__main__":

    data = [
        {
            "input":{‘s‘:‘aa‘,‘p‘:‘a‘},
            "output":False,
        },
        {
            "input":{‘s‘:‘aa‘,‘p‘:‘a*‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘ab‘,‘p‘:‘.*‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘aab‘,‘p‘:‘c*a*b‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘mississippi‘,‘p‘:‘mis*is*p*.‘},
            "output":False,
        },
        {
            "input":{‘s‘:‘aaa‘,‘p‘:‘ab*a*c*a‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘ab‘,‘p‘:‘.*c‘},
            "output":False,
        },
        {
            "input":{‘s‘:‘axb‘,‘p‘:‘a.b‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘mississippi‘,‘p‘:‘mis*is*ip*.‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘aaa‘,‘p‘:‘a*a‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘‘,‘p‘:‘.*‘},
            "output":True,
        },
        {
            "input":{‘s‘:‘aaa‘,‘p‘:‘aaaa‘},
            "output":False,
        },
        {
            "input":{‘s‘:‘a‘,‘p‘:‘ab*‘},
            "output":True,
        }
    ];
    for d in data:

        print(d[‘input‘][‘s‘],d[‘input‘][‘p‘])

        # 计算运行时间
        start = time.perf_counter()
        result=Solution().isMatch(d[‘input‘][‘s‘],d[‘input‘][‘p‘])
        end = time.perf_counter()

        print(result)
        if result==d[‘output‘]:
            print("--- ok ---",end="\t")
        else:
            raise Exception

        print(start-end)

不行,今天大半天都浪费到这上面了,怀疑人生。

去搜索一下,发现:

总结:想法本来就没有成熟,之前的题目都是一些常规的,这个正则不研究没有理论支撑可不好用。

等日后再战

原文地址:https://www.cnblogs.com/warcraft/p/9367415.html

时间: 2024-08-02 05:34:21

失败的尝试 10. regular expression matching & 正则的相关文章

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

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

一.题目说明 这个题目是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

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 10 Regular Expression Matching (C,C++,Java,Python)

Problem: 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

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. The matching should cover the entire input string (not partial). The function prototype should be