#P7064. [NWRRC2014] Expression

[NWRRC2014] Expression

题目描述

In computing, regular expressions is a powerful tool for text search and string matching. In this problem a simplified version of regular expressions is used:

  • An empty string is a regular expression, only the empty string matches it.

  • A single lowercase letter c is a regular expression, a string consisting of a single letter cc matches it.

  • A dot . is a regular expression, a string consisting of any single letter matches it.

  • Alternation: if αα and ββ are regular expressions then (α|β) is a regular expression, a string ss matches it only if ss matches αα or ss matches ββ.

  • Concatenation: if αα and ββ are regular expressions then (αβ) is a regular expression, a string ss matches it only if s=s = xy, xx matches αα and yy matches ββ.

  • Kleene star: if αα is regular expression then (α∗) is a regular expression, a string ss matches it only if ss is empty or s=s = xy, xx is nonempty and matches αα and yy matches (α).(α∗). In other words, ss consists of zero or more strings, each of them matches α.α.

Parentheses can be omitted, in this problem Kleene star has the highest priority, concatenation has medium priority and alternation has lowest priority. Thus abc*|de means (ab(c*))|(de).

For example, string abcabcab matches a(bc|a)*ab, but string abcbab does not.

Your task is to find the shortest string that matches the given regular expression EE and contains the given substring SS .

输入格式

The first line of the input file contains the regular expression EE . The second line of the input file contains the substring S(1E,S10000)S (1 \le |E| , |S| \le 10 000) .

String SS consists of lowercase English letters. Expression EE consists of lowercase English letters and special characters: dots (.), parentheses (() and ()), pipes (|), and asterisks (*).

输出格式

Output the shortest possible string TT that both matches EE and contains SS as substring. If there are no such strings, output NO.

The string TT should contain only lowercase English letters.

题目大意

在计算机中,正则表达式是文本搜索和字符串匹配的强力工具。这道题将使用简化后的正则表达式:

  • 一个空字符串()是一个正则表达式,且只有空字符串匹配它。

  • 一个小写字母(c)是一个正则表达式,只有仅包含一个字母 cc 的字符串匹配它。

  • 点(.)是一个正则运算符,任何仅包含一个字母的字符串都匹配它。

  • 或运算:若 α\alphaβ\beta 是正则表达式,则 ( α\alpha | β\beta ) 是正则表达式;当且仅当字符串 ss 匹配 α\alpha 或 匹配 β\beta 时,ss 匹配 ( α\alpha | β\beta )。

  • 与运算:若 α\alphaβ\beta 是正则表达式,则 ( αβ\alpha\beta ) 是正则表达式;当且仅当字符串 ss 可以分为两个子串 xxyy ,且 xx 匹配 α\alphayy 匹配 β\beta 时,ss 匹配 ( αβ\alpha\beta )。

  • 星号运算:若 α\alpha 是正则表达式,则 ( α\operatorname{\alpha_*} ) 是正则表达式;当且仅当字符串 ss 为空或 ss 可以被分为若干个匹配 α\alpha 的子串时,ss 匹配 ( α\operatorname{\alpha_*} )。

在本题中,括号可以被省略;运算符的优先级从高到低依次为:星号运算、与运算,或运算。所以 ( abcabc_* | dede ) 即 ( abab ( cc_* )) | ( dede ) 。

举个例子,字符串 abcabcababcabcab 匹配 ( aa ( bcbc | aa )ab_*ab ),而字符串 abcbababcbab 并不匹配它。

现给定正则表达式 EE 和字符串 SS ,要求找出最短的匹配 EE 的字符串 TT ,且 TT 必须包含 SS

a.*b
bab

abab

(ab)*
bb

NO

提示

Time limit: 10 s, Memory limit: 256 MB.