#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
-
A single lowercase letter
c
is a regular expression, a string consisting of a single letter 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 matches it only if matches or matches . -
Concatenation: if and are regular expressions then
(αβ)
is a regular expression, a string matches it only ifxy
, matches and matches . -
Kleene star: if is regular expression then
(α∗)
is a regular expression, a string matches it only if is empty orxy
, is nonempty and matches and matches In other words, 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 and contains the given substring .
输入格式
The first line of the input file contains the regular expression . The second line of the input file contains the substring .
String consists of lowercase English letters. Expression consists of lowercase English letters and special characters: dots (.
), parentheses ((
) and ()
), pipes (|
), and asterisks (*
).
输出格式
Output the shortest possible string that both matches and contains as substring. If there are no such strings, output NO
.
The string should contain only lowercase English letters.
题目大意
在计算机中,正则表达式是文本搜索和字符串匹配的强力工具。这道题将使用简化后的正则表达式:
-
一个空字符串()是一个正则表达式,且只有空字符串匹配它。
-
一个小写字母(c)是一个正则表达式,只有仅包含一个字母 的字符串匹配它。
-
点(.)是一个正则运算符,任何仅包含一个字母的字符串都匹配它。
-
或运算:若 和 是正则表达式,则 ( | ) 是正则表达式;当且仅当字符串 匹配 或 匹配 时, 匹配 ( | )。
-
与运算:若 和 是正则表达式,则 ( ) 是正则表达式;当且仅当字符串 可以分为两个子串 和 ,且 匹配 , 匹配 时, 匹配 ( )。
-
星号运算:若 是正则表达式,则 ( ) 是正则表达式;当且仅当字符串 为空或 可以被分为若干个匹配 的子串时, 匹配 ( )。
在本题中,括号可以被省略;运算符的优先级从高到低依次为:星号运算、与运算,或运算。所以 ( | ) 即 ( ( )) | ( ) 。
举个例子,字符串 匹配 ( ( | ) ),而字符串 并不匹配它。
现给定正则表达式 和字符串 ,要求找出最短的匹配 的字符串 ,且 必须包含 。
a.*b
bab
abab
(ab)*
bb
NO
提示
Time limit: 10 s, Memory limit: 256 MB.