#P7064. [NWRRC 2014] Expression

[NWRRC 2014] 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.

a.*b
bab

abab

(ab)*
bb

NO

提示

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