PATT - Pattern Matching

A regular expression is a string which contains some normal characters and some meta characters. The meta characters include:

  • . means any character
  • [c1 - c2] means any character between c1 and c2 (c1 and c2 are two characters).
  • [ˆc1 - c2] means any character not between c1 and c2 (c1 and c2 are two characters).
  • * means the character before it can occur any times.
  • + means the character before it can occur any times but at least one times.
  • \ means any character follow should be treated as normal character.

You are to write a program to find the leftmost substring of a given string, so that the substring can match a given regular expression. If there are many substrings of the given string can match the regular expression, and the left positions of these substrings are same, we prefer the longest one.

Input

Every two lines of the input is a pattern-matching problem. The first line is a regular expression, and the second line is the string to be matched. Any line will be no more than 80 character. A line with only an end will terminate the input.

Output

For each matching problem, you should give an answer in one line. This line contains the string to be matched, but the leftmost substring that can match the regular expression should be bracketed. If no substring matches the regular expression, print the input string.

Example

Input:
.*
asdf
f.*d.
sefdfsde
[0-9]+
asd345dsf
[^\*-\*]
**asdf**fasd
b[a-z]*r[s-u]*
abcdefghijklmnopqrstuvwxyz
[T-F]
dfkgjf
end
Output:
(asdf)
se(fdfsde)
asd(345)dsf
**(a)sdf**fasd
a(bcdefghijklmnopqrstu)vwxyz
dfkgjf

Added by:Andres Tellez
Date:2011-05-17
Time limit:5.454s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

hide comments
2020-04-29 14:45:52 Simes
For more regex problems, see ADV04G and ADV04G1
2017-01-29 13:19:19
PREMATURE EOF ! arrghh. Downvote. Why have an elaborate ending condition when you will just create bad testcases to annoy people. wasted so much time.
2013-07-15 16:10:53 Mitch Schwartz
Ignore empty lines, and watch out for premature EOF as well.
2012-01-09 03:48:23 Pranay
hendrik: thanks..but i considered that too
2012-01-08 16:13:21 hendrik
Pranay: Extra new lines.
2012-01-08 15:22:14 Pranay
@hendrik: i did consider the case when the regex is not valid ,is there any other weird input ?
2012-01-08 10:22:31 hendrik
Pranay: As regards your runtime error it is a known fact that input files are not always perfectly prepared. That is what you have to cope with.

Last edit: 2012-01-08 10:38:35
2012-01-08 08:39:53 Pranay
if there are any invalid regular exp do we just output the input string?
@ProblemSetter: could you please check why my solution(id: 6315582) gives runtime error.
2012-01-07 15:30:29 hendrik
[T-F] is not supposed to match.

Last edit: 2012-01-07 15:33:10
2011-05-18 12:33:43 Oleg
Yes, I just agree with your position and ask problem setter fix example/real test cases.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.