Under construction

Still being built. Things may change

Disjoint Regular Expressions

Time limit2sMemory limit128 MB

Summary
Given two regular expressions, decide whether any non-empty string matches both, and if so print the shortest lexicographically smallest such string.
Level

Hard8 of 10

Topics
Dynamic programming, BFS, String, Simulation
Solved
No attempts yet

Problem

Mark is building a new social network, Facepalm, for the inhabitants of Phobos and Deimos. For each account he wants to suggest a default home asteroid at logon, and he has found that a user's home asteroid can be guessed by analyzing their last name.

Every last name is a non-empty word of lowercase English letters. Last names of users from Phobos match a regular expression PP, while last names of users from Deimos match a regular expression DD.

The trouble is that some last names may match both expressions. The two expressions are called disjoint if there is no non-empty string ss that matches both of them. Mark believes PP and DD are disjoint, but he needs you to check.

Given the two regular expressions PP and DD, decide whether they are disjoint. If they are not, find the shortest non-empty string that matches both of them. If several shortest strings exist, output the lexicographically smallest one.

Input

The input consists of two lines. The first line contains the regular expression PP, and the second line contains the regular expression DD. Each expression has from 11 to 100100 characters.

Output

If the two expressions are disjoint, print Correct on a single line.

Otherwise print Wrong on the first line, and on the second line print the shortest non-empty string that matches both expressions; if several shortest strings exist, print the lexicographically smallest one.

Definitions

Regular expressions and the strings that match them are defined as follows.

  • A single lowercase letter cc is a regular expression; it matches only the one-character string cc.
  • Alternation: if PP and QQ are regular expressions, then (P∣Q)(P \mid Q) is a regular expression; a string α\alpha matches it if α\alpha matches PP or α\alpha matches QQ.
  • Concatenation: if PP and QQ are regular expressions, then (PQ)(PQ) is a regular expression; a string α\alpha matches it if α=βγ\alpha = \beta\gamma where β\beta matches PP and γ\gamma matches QQ.
  • Kleene star: if PP is a regular expression, then (P∗)(P^{*}) is a regular expression; a string α\alpha matches it if α\alpha can be split into zero or more pieces α1α2…αk\alpha_1\alpha_2\dots\alpha_k that each match PP. The empty string always matches a Kleene star.

In the input, expressions are written with parentheses for grouping and the usual operator precedence: the Kleene star * binds most tightly, then concatenation, then alternation |. For example, a(ab)*b means the letter a, followed by zero or more copies of ab, followed by the letter b.

Examples4

  1. Example 1

    Input
    a(ab)*b
    a(a|b)*ab
    
    Expected output
    Correct
    
  2. Example 2

    Input
    a(ab)*a
    a(a|b)*ba
    
    Expected output
    Wrong
    aaba
    
  3. Example 3

    Input
    a
    a
    
    Expected output
    Wrong
    a
    
  4. Example 4

    Input
    a
    b
    
    Expected output
    Correct