改进的带简单反向引用的正则表达式匹配
Improved Regular Expression Matching with Simple Backreferences
- DTU(丹麦技术大学)
机构由 AI 辅助整理,请以论文原文为准。
AI总结:
针对含单个捕获组和k个引用的正则表达式匹配,提出O(n^2m)时间和O(nm)空间的新算法,优于现有结果,并扩展到有序单嵌套类。
AI中文摘要:
带反向引用的正则表达式(rewb)指定了一组由字符通过连接、并、星号运算符和反向引用组合而成的字符串。一个反向引用由一个捕获组 $(\cdot)_i$ 和一个引用 $\backslash i$ 组成。引用匹配的子串必须与相应捕获组匹配的子串相同。给定一个 rewb $R$ 和一个字符串 $Q$,rewb 匹配问题是判断 $Q$ 是否是 $R$ 指定的字符串之一。在完全一般性下,rewb 匹配是 NP 完全的,但对于各种子类存在高效解法。在本文中,我们关注包含单个捕获组和 $k$ 个引用的 rewb。对于此类,Uezato [CPM 2026] 给出了一个 $O(k n^2 m^2)$ 时间和 $O(n^2m^2)$ 空间的算法,其中 $m$ 是正则表达式 $R$ 的长度,$n$ 是字符串 $Q$ 的长度。对于 $k=1$ 的特殊情况,Nogami 和 Terauchi [MFCS 2025] 给出了一个 $O(n^2m^2)$ 时间和 $O(n+ m^2)$ 空间的算法。另一方面,Nogami、Nakamura 和 Terauchi [arXiv 2026] 给出了一个条件下界,表明在正交向量假设下,对于任何 $\epsilon > 0$,我们无法在 $O(n^{2-\epsilon} \mathrm{poly}(m))$ 时间内解决该问题。我们的主要结果是一个新算法,运行时间为 $O(n^2m)$,空间为 $O(nm)$。这分别改进了上述结果(因子为 $km$ 和 $m$)以及前者的空间界(因子为 $nm$)。我们还展示了如何扩展我们的算法以处理稍微更一般的有序和单嵌套 rewb 类。
英文摘要:
A regular expression with backreferences (rewb) specifies a set of strings formed by characters combined with concatenation, union, star operators, and backreferences. A backreference consists of a capturing group $(\cdot)_i$ and a reference $\backslash i$. The substring matched by the reference must match the substring matched by the corresponding capturing group. Given a rewb $R$ and a string $Q$, the rewb matching problem is to decide whether $Q$ is one of the strings specified by $R$. In full generality, rewb matching is NP-complete, but efficient solutions exist for various subclasses. In the paper, we focus on rewb containing a single capturing group and $k$ references. For this class, Uezato~[CPM 2026] gave an $O((k n^2 m^2)$ time and $O(n^2m^2)$ space algorithm, where $m$ is the length of the regular expression $R$ and $n$ is the length of the string $Q$. For the special case of $k=1$, Nogami and Terauchi~[MFCS 2025] gave an $O(n^2m^2)$ time and $O(n+ m^2)$ space algorithm. On the other hand, Nogami, Nakamura, and Terauchi~[arXiv 2026] gave a conditional lower bound, showing that we cannot solve the problem in $O(n^{2-ε} \mathrm{poly}(m))$ for any $ε> 0$ assuming the orthogonal vector hypothesis. Our main result is a new algorithm that runs in $O(n^2m)$ time and uses $O(nm)$ space. This improves the above results (by a factor of $km$ and $m$, respectively) and the former's space bound (by a factor of $nm$). We also show how to extend our algorithm to handle a slightly more general class of ordered and single-nested rewbs.