eolymp
bolt
Try our new interface for solving problems
Məsələlər

BrainFuckVM

BrainFuckVM

Given a brainfuck program, determine whether it terminates or enters an endless loop. A brainfuck interpreter has a data array (consisting of unsigned 8-bit integers) with an index, the so called "data index "; the entry of the array pointed to by the data index is the so called "current entry". A brainfuck program consists of a sequence of the following eight instructions: Interpretation of a brainfuck program starts at the first instruction, and terminates if the instruction pointer leaves the end of the program. After an instruction is executed, the instruction pointer advances to the instruction to the right (except, of course, if the loop instructions \[ or \] cause a jump). In addition to the program, you will be given the size of the data array. The entries of the data array shall be unsigned 8-bit integers, with usual over- or underow behaviour. At the start of the program, the data array entries and the data index shall be initialized to zero. Incrementing or decrementing the data index beyond the boundaries of the data array shall make it re-enter the data array at the other boundary; e.g. decrementing the data index when it is zero shall set it to the size of the data array minus one. The \[ and \] instructions de_ne loops and are allowed to nest. Every given program will be wellformed, i.e. when traversing the program from left to right, the number of \[ instructions minus the number of \] instructions will always be greater or equal zero, and at the end of the program it will be equal to zero. For the purposes of the problem, discard the output of the interpreted program^1. ____________ ^1 For purposes of debugging, you may examine it. The third program from the sample input would print "ICPC"; the fourth program, when given an arbitrary string, will print a brainfuck program, that in turn will print said arbitrary string when executed. \InputFile The input starts with a line containing the number of test cases \textbf{t} (\textbf{0} < \textbf{t} ≤ \textbf{20}). Each test case consists of three lines. The first line contains three integers \textbf{sm}, \textbf{sc}, \textbf{si}, describing the size of the memory, the size of the program code and the size of the input (\textbf{0} < \textbf{sm} ≤ \textbf{100 000}; \textbf{0} < \textbf{sc}, \textbf{si} < \textbf{4 096}). The second line contains the brainfuck program code to be executed; it consists of sc characters. The third line contains the input of the program, as text (only printable, non-whitespace characters). Once the program has consumed all available input, the input instruction shall set the current cell to \textbf{255}. \OutputFile For each test case, print one line, containing either "\textbf{Terminates}" or "\textbf{Loops}", depending on whether the program either terminates after a finite number of steps, or enters an endless loop. If the program loops, also print the indices (\textbf{0}-based) of the two \[ and the \] symbols in the code array that correspond to the endless loop. You may safely assume that after \textbf{50 000 000} instructions, a program either terminated or hangs in an endless loop, which then was executed at least once. During each iteration of the endless loop at most \textbf{50 000 000} instructions are executed.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
4
10 4 3
+-.,
qwe
1000 5 1
+[+-]
a
100 74 4
+++++[->++<]>[-<+++++++>]<[->+>+>+>+<<<<]>+++>--->++++++++++>---<<<.>.>.>.
xxyz
9999 52 14
+++++[>+++++++++<-],+[-[>--.++>+<<-]>+.->[<.>-]<<,+]
this_is_a_test
Çıxış verilənləri #1
Terminates
Loops 1 4
Terminates
Terminates
Mənbə ACM ICPC German Collegiate Programming Contest 2012