【问题标题】:Time complexity for Regular Expression Matching正则表达式匹配的时间复杂度
【发布时间】:2021-07-08 04:03:21
【问题描述】:

我正在解决 leetcode 上的正则表达式匹配问题。我使用递归解决了它,如下所示:

    if (p.empty())    return s.empty();
    
    if ('*' == p[1])
        // x* matches empty string or at least one character: x* -> xx*
        // *s is to ensure s is non-empty
        return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p));
    else
        return !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p.substr(1));

但是在这我怎么能找到代码的时间复杂度呢?

问题链接:https://leetcode.com/problems/regular-expression-matching/

PS:在解决方案中,他们已经解释了时间复杂度,但我无法理解。

【问题讨论】:

    标签: c++ algorithm time-complexity


    【解决方案1】:

    假设T(t,p)是函数isMatch(text, pattern)的时间复杂度,其中t是text.length(),p是pattern.length() / 2

    所有 x 的第一个 T(x,0) = 1

    那么如果pattern[1] == '*'T(t,p) = T(t,p-1) + T(t-1,p) + O(t + p)

    否则T(t,p) = T(t-1, p-0.5) + O(t + p)

    显然第一种情况更糟

    想想T组合含义。 原来是你在坐标(t,p)上的一个球,你可以一步将它移动到(t-1,p)(t,p-1),花费t+p。 球停在轴上。 然后T(t,p) 等于从(t,p) 开始将球移动到轴的每种有效方式的总成本。

    那么我们就知道了

    所以总时间复杂度为O((t+p)2^(t + p/2))

    顺便说一句,如果您使用类似std::string_view 而不是.substr(),您的代码会运行得更快,这会阻止复制整个字符串。

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多