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

Term Generator

Term Generator

A formula has the syntax shown in figure 1(a). If a formula has the particular syntax given in figure 1(b) we say that the formula is in Normal Form (\textbf{NF}). \includegraphics{https://static.e-olymp.com/content/82/8296a57f2e4cad5c0efa1418c414d076850ce035.jpg} \includegraphics{https://static.e-olymp.com/content/08/084a9fa9dfc02a0e461cd733e8423ba7ea3b1094.jpg} A formula is converted to \textbf{NF} according to the rewriting rules given below, where \textbf{F} represents a formula, \textbf{S} stands for a non empty sequence of formulae, and \textbf{s} and \textbf{s'} denote possibly empty sequences of formulae. Applying a rewriting rule \textbf{q} → \textbf{r} on a formula \textbf{F} means to substitute by \textbf{r} a part of \textbf{F} that matches the pattern \textbf{q}, as shown in figure 2. The conversion terminates when no rewriting rule can be applied. The conversion terminates for any formula, and the result is unique regardless which rules are applied on which parts of the formula and in which order. \begin{enumerate} \item \textbf{(+F) }→ \textbf{F} \item \textbf{(*F) }→ \textbf{F} \item \textbf{(+s(+S)s') }→ \textbf{(+sSs')} \item \textbf{(*s(*S)s') }→ \textbf{(*sSs')} \item \textbf{(*s(+FS)s') }→ \textbf{(+(*sFs')(*s(+S)s'))} \end{enumerate} Let \textbf{NF(F)} be the normal form of the formula \textbf{F}. The problem is to write a term generator that for a given formula \textbf{F} and a number \textbf{k} outputs the next \textbf{k} terms from \textbf{NF(F)} in the order in which they appear in \textbf{NF(F)}. If the terms are exhausted, the generator continues from the first term of \textbf{NF(F)}. For example, consider \textbf{F=(+(*(+(*ab)(+a))b))}, and \textbf{NF(F)=(+(*abb)(*ab))} as in figure 2. Generating the first term from \textbf{NF(F)} yields \textbf{(*abb)}. Generating two more terms produces \textbf{(*ab)}, \textbf{(*abb)}. Notice that if \textbf{NF(F) }contains similar terms, as in the last example in sample input/output, these terms are considered distinct. \InputFile Write a term generator that reads sets of data from the standard input. The content of a data set is \textbf{F} \textbf{k_1} ... \textbf{k_N} \textbf{0}, \textbf{n} > \textbf{0}, where \textbf{F} is a formula, and \textbf{k_1} ... \textbf{k_N} are long integers different than \textbf{0}. Each printed term starts from the beginning of a line and there are no white spaces between the characters of the term. The generated terms are not printed if \textbf{k_i < 0}. White spaces are used freely in the input. Each formula \textbf{F} in the input has at most \textbf{150} characters and each term of \textbf{NF(F)} is at most \textbf{80} characters long, not counting white spaces. The input data terminate with an end of file, and are correct. \OutputFile For each \textbf{k_i}, \textbf{i} = \textbf{1}, \textbf{n}, the program generates the next |\textbf{k_i}| terms from \textbf{NF(F)} and, if \textbf{k_i} >\textbf{ 0}, prints these terms on the standard output.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Источник SEERC-2011