This page is still under construction.

Parts of this page are still being built. What you see may change.

L-system Substring

Time limit1sMemory limit128 MB

Summary
Given a D0L system over {a,b} and a query z, decide whether z appears as a contiguous substring of some word derivable from the start word.
Level

Hard8 of 10

Topics
String, Simulation, Implementation, Brute force
Solved
No attempts yet

Problem

A D0L system (a deterministic Lindenmayer system without interaction) consists of a finite alphabet Σ\Sigma, a finite set of productions PP, and a starting string ww. Every production has the form x→ux \to u, where x∈Σx \in \Sigma and u∈Σ+u \in \Sigma^{+} (a nonempty string over Σ\Sigma); for each symbol x∈Σx \in \Sigma the set PP contains exactly one production whose left side is xx.

A direct derivation turns one string into another by simultaneously replacing every symbol of the string with the right side of its production. The language of the system is the set of all strings that can be obtained from ww by applying zero or more direct derivations (so ww itself belongs to the language).

Here the alphabet is Σ={a,b}\Sigma = \{a, b\}, so there are exactly two productions, a→ua \to u and b→vb \to v with u,v∈{a,b}+u, v \in \{a, b\}^{+}, and the starting string is w∈{a,b}+w \in \{a, b\}^{+}.

Given a string zz, decide whether the language contains at least one string of the form x z yx\,z\,y with x,y∈{a,b}∗x, y \in \{a, b\}^{*} — equivalently, whether zz occurs as a contiguous substring of some string in the language.

Input

The input contains several blocks and ends at end of file; there are no blank lines between consecutive blocks. Each block consists of exactly four lines:

  1. the right side uu of the production a→ua \to u;
  2. the right side vv of the production b→vb \to v;
  3. the starting string ww;
  4. the query string zz.

Each of these four strings is nonempty, consists only of the letters aa and bb, and has length at most 1515.

Output

For each block, print a single line containing YES if some string in the language contains zz as a substring, and NO otherwise.

Examples3

  1. Example 1

    Input
    aa
    bb
    ab
    aaabb
    a
    b
    ab
    ba
    
    Expected output
    YES
    NO
    
  2. Example 2

    Input
    aa
    bb
    ab
    aabb
    
    Expected output
    YES
    
  3. Example 3

    Input
    a
    b
    aab
    ba
    
    Expected output
    NO