问题 1098 --L-system

1098: L-system

时间限制: 1 Sec  内存限制: 128 MB
提交: 0  解决: 0
[提交][状态][讨论版][命题人:]

题目描述

A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite set tex2html_wrap_inline36 of symbols (the alphabet), a finite set P of productions and a starting string tex2html_wrap_inline40 . The productions in P are of the form tex2html_wrap_inline44 , where tex2html_wrap_inline46 and tex2html_wrap_inline48 (u is called the right side of the production), tex2html_wrap_inline52 is the set of all strings of symbols from tex2html_wrap_inline36 excluding the empty string. Such productions represent the transformation of the symbol x into the string u. For each symbol tex2html_wrap_inline46 , P contains exactly one production of the form tex2html_wrap_inline44 . Direct derivation from string tex2html_wrap_inline66 to tex2html_wrap_inline68 consists of replacing each occurrence of the symbol tex2html_wrap_inline46 in tex2html_wrap_inline66 by the string on the right side of the production for that symbol. The language of the D0L system consists of all strings which can be derived from the starting string tex2html_wrap_inline40 by a sequence of the direct derivations.

Suppose that the alphabet consists of two symbols a and b. So the set of productions includes two productions of the form a tex2html_wrap_inline76 , b tex2html_wrap_inline78 , where u and tex2html_wrap_inline82 , and the starting string tex2html_wrap_inline84 . Can you answer whether there exists a string in the language of the D0L system of the form xzy for a given string z? (x and y are some strings from tex2html_wrap_inline94 , tex2html_wrap_inline94 is the set of all strings of symbols from tex2html_wrap_inline36 , including the empty string.). Certainly you can. Write the program which will solve this problem.

输入

The input file of the program consists of several blocks of lines. Each block includes four lines. There are no empty lines between any successive two blocks. The first line of a block contains the right side of the production for the symbol a. The second one contains the right side of the production for the symbol b and the third one contains the starting string tex2html_wrap_inline40 and the fourth line the given string z. The right sides of the productions, the given string z and the starting string tex2html_wrap_inline40 are at most 15 characters long.

输出

For each block in the input file there is one line in the output file containing YES or NO according to the solution of the given problem.

样例输入

aa
bb
ab
aaabb
a
b
ab
ba

样例输出

YES
NO

提示

来源

 

[提交][状态]