Problem G
A=B

After hours of playing the hit puzzle game A=B, you’ve become an expert at transforming strings using substitution rules. In A=B, players are challenged to manipulate strings using a minimalistic programming language that consists solely of substitution instructions of the form ‘A=B’. Each instruction replaces occurrences of string ‘A’ with string ‘B’, allowing players to solve various problems—from simple character replacements to more complex transformations like multiplying binary numbers. Inspired by the game’s elegant simplicity and the power of its single operation, you had an epiphany: “I could totally make this myself!”
With newfound inspiration, you decide to build your own version of the A=B interpreter. Your version will follow a strict sequence of substitution rules, carefully applying them one at a time until the string can’t change any further—or until it grows out of control! You decide to include safety limits for memory and time, so your interpreter won’t run away in an endless loop.
A=B’s interpreter works as follows: first, it reads a
starting string, as well as an ordered list of rules to apply.
Each rule is a string of the form ‘
One step of your interpreter consists
of finding the first rule,
If your interpreter does not find any rule that can be applied, then it should exit and output the current string.
If, after
If at any point the length of the current string exceeds
Input
The first line contains the starting string
The second line contains an integer
The next
Output
Print “Time Limit Exceeded” if the
interpreter could perform more than
Print “Memory Limit Exceeded” if
the string length exceeds
Otherwise, print the value of the final string.
Sample Input 1 | Sample Output 1 |
---|---|
CBACAB 3 BA=AB CA=AC CB=BC |
AABBCC |
Sample Input 2 | Sample Output 2 |
---|---|
A 1 A=AA |
Memory Limit Exceeded |
Sample Input 3 | Sample Output 3 |
---|---|
AB 2 AB=C C=AB |
Time Limit Exceeded |