eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Out of context

Out of context

It’s that time of year: election season. Political speeches abound, and your friend the armchair pundit likes to find quotes of politicians and use them out of context. You want to help your friend by developing a method to search through text for specified patterns. \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} One of the more powerful ways to express a text search pattern is using a context-free grammar (\textbf{CFG}). A \textbf{CFG} is used to generate strings, and is defined as a \textbf{4}-tuple (\textbf{V}, \textbf{Σ}, \textbf{R}, \textbf{S}) where \textbf{V} is a set of variables, \textbf{Σ} is a set of terminal symbols, \textbf{S} \textbf{V} is the starting variable, and \textbf{R} is a set of rules. Each rule in \textbf{R} is of the form \includegraphics{https://static.e-olymp.com/content/7d/7da7bc3a35a7619a6f1619289d7c2bd8007ad80c.jpg} which indicates that the head of the rule (the variable to the left of the arrow) can be replaced whenever it appears by the rule’s production, a sequence of variables and terminal symbols on the right side of the arrow. It is possible for the right side of a rule to be empty. This indicates that the variable on the left can be replaced by the empty string. A grammar generates a string of terminals by the process of \textit{derivation}. A derivation begins with a sequence that is just the start variable. Then, until all variables have been removed, we repeatedly replace any variable in the current sequence by any one of that variable’s rules. As an example, here are rules for a grammar with start variable \textbf{A} (left), and an example derivation (right). \includegraphics{https://static.e-olymp.com/content/68/682422b7cb5ec12523d067a61be6b3499fdf74ee.jpg} Write a program that can search through text for substrings that could have been generated by a given \textbf{CFG}. \InputFile For this problem, \textbf{V} is the set of English uppercase alphabet letters and \textbf{Σ} is the set of English lowercase alphabet letters. Input begins with a description of \textbf{R}, the rules of the \textbf{CFG}. The first line contains an integer \textbf{1} ≤ \textbf{n} ≤ \textbf{30}, indicating the number of rules to follow. The next n lines each describe one rule, in the format \includegraphics{https://static.e-olymp.com/content/82/82e19565b48a65751c0aeac3ed0f75cdb46712d0.jpg} That is, an uppercase letter, a single space, an arrow, a single space, and a string of zero or more uppercase or lowercase letters. Rule productions are at most \textbf{10} characters long. The start variable \textbf{S} is the head of the first rule given. Following the rules are at most \textbf{100} lines of text to search, each of length at most \textbf{50} characters. Each line of input consists of only English lowercase letters and spaces. Input ends at end of file. \OutputFile For each line to search, print a line giving the longest non-empty substring that the grammar can generate. If there is a tie for the longest, give the substring that appears earliest on the line. If there is no matching substring, print \textbf{NONE} in capital letters.
Ліміт часу 1 секунда
Ліміт використання пам'яті 256 MiB
Вхідні дані #1
5
S -> aSa
S -> bSb
S -> a
S -> b
S -> 
where are the abaaba palindromes on this line
none on this line
how about this aaaaaaabbbbbbbbbbbbbbbbba
even a single a or b is a palindrome
Вихідні дані #1
abaaba
NONE
abbbbbbbbbbbbbbbbba
a
Джерело 2012 ACM-ICPC North American Qualification Contest