問題一覧 > 通常問題

No.874 Distance Between Regular Expressions

レベル : / AI推定難易度 : 1800 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 41
作問者 : kakira9618 / テスター : pockyny
お気に入りにしたユーザー ProblemId : 2776 / 自分の提出
問題文最終更新日: 2019-08-15 23:23:35
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 221 (順位表) の他の問題:

Problem Statement

You are given strings $A$, $B$, each of which is a regular expression (of a restricted form) consisting of ?, *, and lowercase letters.
Here, ? denotes zero or one repetition of the preceding character, and * denotes zero or more repetitions of the preceding character.
For $A$, $B$, let $G_A$, $G_B$ respectively be the set of strings generated by the regular expression.
For example, when $A = $ "a?b*", we have $G_A=\{$"", "a", "b", "ab", "bb", "abb", "bbb", "abbb", $\cdots \}$.

For strings $a$ and $b$ consisting only of lowercase letters, the edit distance $d(a, b)$ between $a$ and $b$ is defined as
"the minimum number of steps required to transform $a$ into $b$ by single-character insertions, deletions, and substitutions"
.
For example, consider the edit distance between "abcdefgh" and "zcdefgha". For "abcdefgh",
deleting a, replacing b with z, and adding a transforms it into "zcdefgha",
and since this is the minimum number of steps, we have $d("$abcdefgh$", "$zcdefgha$") = 3$.

The distance between regular expressions $D(A, B)$ for $A$, $B$ is defined as $\min_{(a, b) \in G_A \times G_B } d(a, b)$.
In other words, $D(A, B)$ is defined as "the minimum edit distance over all combinations of strings that can be generated by A and B, respectively".
Find $D(A, B)$.

Input

$A$
$B$

$A$, $B$ are strings consisting of ?, *, and lowercase letters.
? and * do not appear consecutively in $A$ or $B$ (they do not contain "??", "?*", "*?", or "**" as a substring).
The first character of $A$, $B$ is neither ? nor *.
$1 \leq |A| \leq 2000$
$1 \leq |B| \leq 2000$

Output

Print $D(A, B)$ on one line.
Print a newline at the end.

Sample

Sample 1
Input
ab?c
abc
Output
0

We have $G_A = \{$ "ac", "abc" $\}$, $G_B = \{$ "abc" $\}$.
Since $d($"ac", "abc"$) = 1$, $d($"abc", "abc"$) = 0$, print 0.

Sample 2
Input
a*bcd
aaaabd?
Output
1

For example, taking $a =$ "aaaabcd", $b =$ "aaaabd" gives an edit distance of 1, which is the minimum.

Sample 3
Input
aaaaabbbaaaaa
a*b?c*
Output
3

Resisting the sweet temptation of "b?" and taking $b =$ "aaaaaaaaaaaaa" gives an edit distance of 3, which is the minimum.

Sample 4
Input
aaaaabbbaaaaa
a*b?a*
Output
2

Accepting the offer of "b?" and taking $b =$ "aaaaaabaaaaaaa" gives an edit distance of 2, which is the minimum.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。